Splunk Dev

Splunk JavaScript help

zacksoft_wf
Contributor

I have a dashboard , where I want some help in developing  a java script that will perform validation on my field.
I have a field named 'url'. I want users to input only values that doesn't have any padded spaces in the starting or ending and it shouldn't also contain  <doublequote>".
I could use some help in building a Java Script to achieve this.

So, far I have created a field ID

<input type="text" token="url" id="url_value" searchWhenChanged="true">    <label>URL</label></input>


In the Splunk XML I have invoked the .js file

<form script="validate the field.js">

I do need some help in writing the javascript that could check for padded spaces in starting or beggining and also to check for " double quote . So that user would avoid inputing this things in the field.

require(["jquery", "splunkjs/mvc/simplexml/ready!"], function($) {
$("[id^=url_value]")
.attr('type','number')

....don't know what next to write here in the .js

 

Labels (1)
0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@zacksoft_wf 

Can you please try this?

<form script="a.js">
  <label>Url Validation</label>
  <fieldset submitButton="false">
    <input type="text" token="tkn_url" id="tkn_url_id">
      <label>URL</label>
    </input>
  </fieldset>
</form>

 

a.js

require([
    'underscore',
    'splunkjs/mvc',
    'jquery',
    "splunkjs/mvc/simplexml/ready!"
], function(_, mvc, $) {
    var tkn_url = splunkjs.mvc.Components.getInstance("tkn_url_id"); // your multiselect id here
    tkn_url.on("change", function(e) {
        console.log(e)
            // e.preventDefault();
        if (!isUrlValid(e)) {
            alert("Enter Valid URL")
            return false;
        }
    })

    function isUrlValid(userInput) {
        console.log(userInput)
        var res = userInput.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
        if (res == null)
            return false;
        else
            return true;
    }

})

 

 

 

Thanks
KV
▄︻̷̿┻̿═━一   😉

If any of my reply helps you to solve the problem Or gain knowledge, an upvote would be appreciated.

0 Karma

zacksoft_wf
Contributor

@kamlesh_vaghela 
Hi Kamlesh,
I see the line 

userInput.match(


The regex inside it appears to be matching with a URL field ..starting with Https:/ etc...
But in my case the URL field has a different meaning , the field could contain any string , it doesn't necessarily have to be a web url address.  
Can we modify the regex , so that it matches for
 - no space padded  in the beginning of the string
- no space padded in the end of the string
- There should  not be any double quotes sign "  entered in the string.

If any of the above matches, it should prompt the user to re-enter the value. 
User should keep in mind not to enter strings with spaces in front or end or add any " sign in the input string.

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@zacksoft_wf 

This is an example code, you can change the validation Logic as per your requirements. Just use this code in your JS 🙂

 

KV

0 Karma

zacksoft_wf
Contributor

@kamlesh_vaghela How do I stop the dashboard button from submitting, if there is a validation error message ?  I get the validation error message , But even if I don't fix the entry, it still allows me to submit that record. 

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!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...