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;
}
})
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$)
@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;
Perhaps more like this
var res = userInput.match("(^\s|\"|\s$)")
@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;
}
})
==================================
It looks right to me but bear in mind, I don't know the syntax for javascript.
@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$)");
Try like this
var res = userInput.match(/(^\s|\"|\s$)/g)