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
Get Updates on the Splunk Community!

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...