All Apps and Add-ons

Sideview Utils: How to validate an IP address in the TextField module in Advanced XML?

mythily
Engager

Hi ,

I have an view written in advanced XML using Sideview Utils text field module. I want to validate an IP address in the text field. Is there any way that I can do this?

sideview
SplunkTrust
SplunkTrust

This has come up from time to time and the best way to do it is with a "customBehavior". There is a hidden docs page about customBehaviors - to read it go to "Tools > Other Tools", then scroll down to the other page to the bottom-most panel.

Notes:

-- Since the specific implementation here uses Splunk's core messaging, you need to have a Message module in the view. Here I've included it right above to the TextField but you can put it anywhere on the page.

<module name="Message" layoutPanel="panel_row1_col1">
  <param name="filter">customValidator_1</param>
  <param name="maxSize">1</param>
  <param name="clearOnJobDispatch">True</param>
</module>

<module name="TextField" layoutPanel="panel_row1_col1">
      <param name="name">ip_address</param>
      <param name="label">IP</param>
      <param name="customBehavior">customInputValidation</param>

and then to define the actual customBehavior, the following Javascript has to go in $SPLUNK_HOME/etc/apps/YOUR_APP/appserver/static/application.js

if (typeof(Sideview)!="undefined") {
    Sideview.utils.declareCustomBehavior ("customInputValidation", function(textFieldModule) {
        var messenger = Splunk.Messenger.System.getInstance();
        textFieldModule.validate = function() {
            var v = this.input.val();
            return (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(v));
        };
        textFieldModule.onValidationFail = function() {
            messenger.send("info","customValidator_1", "You must enter a valid IP Address.");
        };
        textFieldModule.onValidationPass = function() {
            messenger.clear();
        };
    });
}

This example was quickly derived from a slightly different working example that ships with Sideview Utils, in case you wanted to look at that one too. You can navigate to that view by going to /en-US/app/sideview_utils/testcases_for_textfield.xml

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Character substitutions with Regular Expressions

This challenge was first posted on Slack #puzzles channelFor BORE at .conf23, we had a puzzle question which ...

Splunk Community Badges!

  Hey everyone! Ready to earn some serious bragging rights in the community? Along with our existing badges ...

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...