Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

validating IP address URL in catalog variable

Amit Dey1
Tera Contributor

Hi I have a requirement to validate this type of IP address in catalog variable , example - http://10.68.6.238 , so which regular expression to use for this 

1 ACCEPTED SOLUTION

Yes, try this

var reipurl = /^https?:\/\/(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/;

View solution in original post

8 REPLIES 8

lundsholm
Mega Expert

Here's an example that I found on https://www.oreilly.com/library/view/regular-expressions-cookbook/9780596802837/ch07s16.html:

 

var regex = /^http:\/\/(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/;

 

 

You can use RegExr to try it out:

https://regexr.com/

 

lundsholm_0-1668513242065.png

 

hi , this regular expression is not working 

Hello, please share your code/implementation details and I'll be happy to help.

Hi lundsholm , there was already a catalog onsubmit  client script which was only getting URL field validation for that particular field , I just add your regex on that OR condition - bold parts are changes done by me

script below -

function onSubmit() {
var data = '{"fields":[' +
'{"fieldName":"cat_variable_name"}]}';

var json = JSON.parse(data);
var isValid = true;
var fieldValuefields = [];
var fieldName = '';
for (var i = 0; i < json.fields.length; i++) {
fieldName = json.fields[i].fieldName;
var fieldValue = g_form.getValue(fieldName);
g_form.hideFieldMsg(fieldName, true);
var varValue = g_form.getValue(fieldName);
var re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
var reipurl = /^http:\/\/(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/;
if (fieldValue.toLowerCase().trim() == "na" || fieldValue.toLowerCase().trim() == "n/a") {
g_form.hideErrorBox(fieldName);
}
else if ((!re.test(fieldValue) || !reipurl.test(fieldValue)) && (fieldValue != '')) {
g_form.showErrorBox(fieldName, "Please enter valid URL");
fieldValuefields.push(g_form.getLabelOf(fieldName));
isValid = false;
} else {
g_form.hideErrorBox(fieldName);
}
}

if (isValid == false) {

alert("Please check URL for fields " + "'" + fieldValuefields + "'", 'error');
return false;
}