How to validate IP Address

Amit Kumar6
Tera Contributor

Hi,

I have requirement for validating the IP Address in Catalog item in the format (0-235).(0-235).(0-235).(0-235)

Kindly suggest me how can i achieve this.

Thanks

6 REPLIES 6

Swapnil Bhakar1
Mega Guru

Hi Amit,



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


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



Hope it helps you.



Regards,


Swapnil



PS: Hit like, Helpful or Correct depending on the impact of the response


tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Amit,


As Swapnil mentions you can use a reg ex.



there is a regex here for IPv4


How should I validate the IP Address in below format?


and some help here for IPv6


Re: Is it possible to validate fields on a character basis?



Best Regards



Tony


BALAJI40
Mega Sage

Hi Amit,



Sometime back i have done the same requirement, Please see the below code and do modification as you like, first i created a script include and called this onchange sync ajax call.



Iam sharing the validations i have done



IpValidationClientSide: function() { // This will use in Client Side



  var ipAddWithDots = this.getParameter('sysparm_ip').toString();



  var ipLength = ipAddWithDots.length();



  if (ipLength> 16){ //this for maximum legth is 15 (235.235.235.235)


  gs.log('This Script Include if loop is Executing:'+ipLength);


  bool = false;.


  return bool;


  }



var iparray = ipAddWithDots.split('\\.');//(To split ip array with dots.



if(iparryLength != 4){ (if you split with dots length is 4)


  bool = false;


  return bool;


  }



  for( i = 0;i < iparryLength ; i++){ //check for it contains any alpha numeric or negative values


  gs.log('AAAA:This Script Include get for loop is executing');


  if (isNaN(iparray[i]) || Number(iparray[i])<0 || Number(iparray[i])>=256){


  gs.log('AAAA:This for-if loop is executing');


  bool = false;


  return bool;


  }



Then call the onchnage catalog client script.


Kalaiarasan Pus
Giga Sage

Add this in a client script



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(ipaddress))


  {


  return (true);


  }


  alert("You have entered an invalid IP address!");


  return (false);



ipaddress should contain the input value of IP entered by the user.