- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Splunk JavaScript help
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
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content


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.
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content


This is an example code, you can change the validation Logic as per your requirements. Just use this code in your JS 🙂
KV
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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.
