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.

How to validate valid IP address format for field in the service catalog

Santoshi Parit
Giga Contributor

Hi All, 

I am new in this enviornment,

My requirement is : I have one field name :IP address this can accept only this type of  format XXX.XXX.XXX.XXX(12 Digit and 3 dots ).

I have tried to use the pattern in client script,Unable to understand how its work.

Many thanks in advance.

 

1 ACCEPTED SOLUTION

Its working, Need to do few changes to get eaxct result , i have done some changes in this script 

need to add ^ and $ to avoid starting and end point characters(ex:...12.12.12.12....)

g_form.clearValue('ip_address',''); :to clear the 

 

Client script:

\\

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var ans = g_form.getValue('ip_address');


var reg = /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/;


if(!reg.test(ans))


{


alert('Please enter correct IP Address');

g_form.clearValue('ip_address','');
// g_form.setValue('ip_address');


}
}

View solution in original post

13 REPLIES 13

Vishal Khandve
Kilo Sage

Hi Santoshi,

 use below script:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

g_form.hideFieldMsg('varaibleName','true');
var ips = g_form.getValue('varaibleName');
var ips_split = ips.split(',');
for (var n = 0; n < ips_split.length; n++) {
var ips_info=ips_split[n].replace(/\s/g,'');
if (!isIP(ips_info)){
g_form.showFieldMsg('varaibleName','List contains an invalid IP address', 'error',false);
}
}
function isIP(ip)
{
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ip))
{
return(true);
}

return(false);
}

}

 

Thanks,

Vishal

Thanks Vishal, 

Its working but when we enter invalid IP address its getting error and once i clicked on Order now ticket get created , 

find_real_file.png

In this Screenshot if you can see the variable editor :

variable IP address is filled incorrectly.

Tushar Jain
Kilo Contributor

If the IP Address is of the form - 0-255.0-255.0-255.0-255, then 

Client Script:-

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ans = g_form.getValue('virtual_ip');
var reg = /^(?=\d+\.\d+\.\d+\.\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}$/;
if (!reg.test(ans)) {
alert('Please enter correct IP Address');
g_form.clearValue('virtual_ip', '');
// g_form.setValue('virtual_ip');
}
}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks.

Tushar