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
Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...