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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...