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

AbhishekGardade
Giga Sage

Hello Santhoshi,

To validate IP address in catalog item you need to create catalog onchange client script which will validate your ip address format.

You can use regular expression to validate the IP address format.

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

  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.setValue('fieldname');

}

Alernative solution:

https://community.servicenow.com/community?id=community_question&sys_id=3862788ddb69af004abd5583ca96...

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

 

Thank you,
Abhishek Gardade

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');


}
}

Thanks for your help!!!!

Its perfectly working now :))

thank you so much @Santoshi Parit this worked for me as well 🙂