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!

Casting Call: Compete in Cyber Games

Lights, Camera, SecOps: Apply to Compete in Cyber Games     Think you have what it takes to beat the clock? ...

Data Management Digest – June 2026

Welcome to the June 2026 edition of Data Management Digest! This month’s update is short and sweet, with a ...

Think Like an Architect: Introducing the Splunk Certified Cybersecurity Defense ...

In cybersecurity, defenders respond to threats. Architects design the systems that stop them.    As ...