Dashboards & Visualizations

RegEx Match Help

zacksoft_wf
Contributor

I have a javascript that I will be invoking from a dashboard to perform validation on a field input of user input, such that the field shouldn't contain any doublequotes OR shouldn't contain any padded spaces in the beginning or the end of the string.  Need help with the regex to match the above condition.

The script looks like this ,

<form script="field_validation.js">
<label>Url Validation</label>
<fieldset submitButton="false">
<input type="text" token="tkn_fld" id="tkn_fld_id">
<label>URL</label>
</input>
</fieldset>
</form>
====================================

field_validation.js 

require([
'underscore',
'splunkjs/mvc',
'jquery',
"splunkjs/mvc/simplexml/ready!"
], function(_, mvc, $) {
var tkn_url = splunkjs.mvc.Components.getInstance("tkn_fld_id"); 
tkn_fld.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( NEED HELP TO WRITE THE REGEX HERE );
if (res == null)
return false;
else
return true;
}

})

Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Perhaps more like this

var res = userInput.match("(^\s|\"|\s$)")

View solution in original post

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

It might be easier to change your logic around and look for spaces at the beginning, spaces at the end or double quotes, and if you get a match return false.

(^\s|\"|\s$)
0 Karma

zacksoft_wf
Contributor

@ITWhisperer  Thank you.
By changing the logic, do you mean something like this.. ?

======
function isFieldValid(userInput) {
console.log(userInput)
var res = userInput.match(^\s|\"|\s$);
if (res == null)
return true;
else
return false;

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Perhaps more like this

var res = userInput.match("(^\s|\"|\s$)")
0 Karma

zacksoft_wf
Contributor

@ITWhisperer 

Sorry for the bother, But could you have a quick glance if the logic is okay, with the added regex.Please !

===========================

require([
'underscore',
'splunkjs/mvc',
'jquery',
"splunkjs/mvc/simplexml/ready!"
], function(_, mvc, $) {
var tkn_value = splunkjs.mvc.Components.getInstance("tkn_value_id");
tkn_value.on("change", function(e) {
console.log(e)
// e.preventDefault();
if (!isValueValid(e)) {
alert("Enter Valid Value")
return false;
}
})

function isValueValid(userInput) {
console.log(userInput)
var res = userInput.match("(^\s|\"|\s$)");
if (res == null)
return true;
else
return false;
}

})

==================================

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

It looks right to me but bear in mind, I don't know the syntax for javascript.

0 Karma

zacksoft_wf
Contributor

@ITWhisperer 
I tested the regex, 
It matches when it finds a doublequote ,
but  it cannot match spaces in the beginning or end.


var res = userInput.match("(^\s|\"|\s$)");

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Try like this

var res = userInput.match(/(^\s|\"|\s$)/g)

zacksoft_wf
Contributor

Thank you so much.

Tags (1)
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!

Cisco Data Fabric from Architecture to Investigation, Better SOC Visibility, and More ...

Splunk Lantern is Splunk’s customer success center that provides practical guidance from Splunk experts on key ...

The Trust Gap: Why a Data Foundation is Fundamental to an Agentic Enterprise

The Trust Gap: Why a data foundation is fundamental to an  Agentic Enterprise.   Agentic AI is transforming ...

Data Management Digest – September 2026

    Welcome to the September 2026 edition of Data Management Digest! September brought a fresh wave of ...