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!

New Case Study Shows the Value of Partnering with Splunk Academic Alliance

The University of Nevada, Las Vegas (UNLV) is another premier research institution helping to shape the next ...

How to Monitor Google Kubernetes Engine (GKE)

We’ve looked at how to integrate Kubernetes environments with Splunk Observability Cloud, but what about ...

Index This | How can you make 45 using only 4?

October 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with this ...