Dashboards & Visualizations

Text Input Validation in Splunk Form Using JavaScript (for Specific Special Characters and IPaddress)

New Member

Hi All,

I have a form with two text inputs.
One should validate and allow users to enter alphanumeric and few specific special characters as in the below JS code.
The other should validate whether the user entered is valid IPaddress or not.

I have written the below validation.js code and deployed it to appserver/static and modified the xml code of form to include "id" field as I have found in Splunk docs.

I have restarted my Splunk environment and tried _bump. But there's no validation happening with inputs.

Is it not invoking the JS script at all? Is something wrong with the way I have defined the JS and xml?

<form script="validation.js">
   <fieldset submitButton="true" autoRun="false">
     <input type="text" token="URLs" id="URLs_only">
       <label>Enter URL</label>
     <input type="text" token="IPs" id="IPs_only">>
       <label>Enter IP</label>
           <query>| makeresults 
  | eval Text="$URLs$, Text1="$IPs$
  | table Text Text1</query>
         <option name="count">20</option>
         <option name="dataOverlayMode">none</option>
         <option name="drilldown">cell</option>
         <option name="percentagesRow">false</option>
         <option name="rowNumbers">false</option>
         <option name="totalsRow">false</option>
         <option name="wrap">true</option>

require(["underscore", "jquery", "splunkjs/mvc", "splunkjs/mvc/simplexml", "splunkjs/mvc/simplexml/ready!",
        "splunkjs/mvc/utils", "splunkjs/mvc/tokenutils" ], function(_, $, mvc, utils, TokenUtils) {

 $('#URLs_only').on("change:url", function() {
    var tokens = mvc.Components.get("default");
    var urlvalue = tokens.get("URLs");
    if( /[^a-zA-Z0-9\.\,\:\-\/]/.test( urlvalue ) ) {
            alert('Input is not valid for URL');
            return false;
            return true;

 $('#IPs_only').on("change:ipadr", function() {
    var tokens = mvc.Components.get("default");
    var ipvalue = tokens.get("IPs");
      if(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test( ipvalue ) ) {
            return true;
            alert('Input is not valid for IpAddress');
            return false; }


Thanks all for your help in advance.

0 Karma

Splunk Employee
Splunk Employee

The problem is your jQuery selector in your JavaScript code. When Splunk renders the dashboard, the ID may not be associated with the input element depending on your Splunk version. For instance, here is a snippet of how your dashboard rendered on Splunk 7.1.2:

<div class="splunk-view splunk-textinput" id="URLs_only_10923" data-view="splunkjs/mvc/textinputview">
    <div data-size="medium" data-test="text" data-test-value="" data-component="splunk-core:/splunkjs/mvc/components/TextInput" class="main_Box_c14abce54916ca6a_f7b2da main_Text_cd96be7d8632d6e1_f7b2da">
        <input type="text" class="input_Text_cd96be7d8632d6e1_f7b2da" value="" aria-multiline="false" data-test="textbox" autocomplete="on" role="textbox" tabindex="0">

Notice the ID is on a <div> instead of an input. Also, notice that the ID was modified. The following modification to your JavaScript should help:

    .on("change", function() {

This jQuery selector is looking for an ID that starts with URLs_only (which will be your <div>). Then, it looks for the input element within that element and carries on.

0 Karma


@ch_soumya, <eval> tag inside Search Event Handlers can be used to perform JavaScript Regular Expression validation in Simple XML itself. Are you sure you want to perform the validation in separate JavaScript static file?

Refer to one of my older answers: https://answers.splunk.com/answers/548736/taking-a-numerical-text-input-for-dashboard-to-nar.html

Alternatively, if you want to perform validation on Text input without using JavaScript static file, you can also use text box input value as token in an independent <search> to perform validation and set final token only if validation passes!

| makeresults | eval message= "Happy Splunking!!!"
0 Karma


Just to be sure, you placed the .js file in the same app as the dashboard? Have you checked the browser console (typically F12) for errors?

0 Karma
.conf21 CFS Extended through 5/20!

Don't miss your chance
to share your Splunk
wisdom in-person or
virtually at .conf21!

Call for Speakers has
been extended through
Thursday, 5/20!