Ankur Bawiskar
Tera Patron

Many a times you would require validation for IP Address if there is a field on the form.

  • A valid IPv4 address should be in the form of xxx.xxx.xxx.xxx, where xxx is a number from 0-255.
  • Class A IP addresses are networks belonging from 1.0.0.0 to 127.0.0.0. Class B is networks 128.0.0.0 through 191.255.0.0. Class C is 192.0.0.0 through 223.255.255.0
  • IP Address 0.0.0.0 is valid since it contains four octets, each within the range 0 through 255 inclusive. However, it's not usable as a real IP address.
  • Valid IP Address always are in between the extreme values, "0.0.0.0" and "255.255.255.255" excluding these extreme values.

For example below are few valid IPv4 Addresses:

10.10.10.10

192.168.1.1

Invalid IPv4 Addresses:

192.168.1.256

255.245.276.243

There exists already a field type IP Address; you can try using that.

Here is the onChange Script on the field:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	var val = validateIP(newValue);
	
	if(val == 'reserved'){
		alert('This is a reserved IP address so cannot be used');
		g_form.clearValue('fieldName');
	}
	else if(!val){
		alert('Please enter valid IP Address');
		g_form.clearValue('fieldName');
	}
	
}
function validateIP(ip) {
	alert('inside function');
	
	if(ip == '0.0.0.0' || ip == '255.255.255.255')
		return 'reserved';
	
	//Check Format
	var ip = ip.split(".");
	
	if (ip.length != 4) {
		return false;
	}
	
	//Check Numbers
	for (var c = 0; c < 4; c++) {
		//Perform Test
		if ( ip[c] <= -1 || ip[c] > 255 ||
			isNaN(parseFloat(ip[c])) ||
		!isFinite(ip[c])  ||
		ip[c].indexOf(" ") !== -1 ) {
			
			return false;
		}
	}
	return true;
}

Kindly do not forget to like or bookmark this post if it helps you.

Kindly input your suggestions if any once you use this.

Comments
Tera Contributor

HI Ankur. Great work. 

How has this been with upgrades (New York/Orlando)? 

Tera Patron
Tera Patron

Thanks for reading the blog.

I had tested this in Madrid. Didn't get a chance to check in NewYork/Orlando

Kindly mark the blog as helpful and don't forget to bookmark

Regards
Ankur

Tera Contributor

Hi Ankur,

 

How can we restrict subnet like 10.10.10.10/25.

Thanks,

Kumar

Tera Patron
Tera Patron

H Kumar,

I believe you will have to write custom script and enhance it if user is giving IP along with subnet

Regards
Ankur

Tera Contributor

Hi Ankur,

 

here i am not using ip Address we are going with subnet only so users may give 192.168.1.0/24 this type of values , how can we avoid spaces as well in between the 192. 168.1 .0/ 24, if they given something like with spaces.

regex= /^[0-9]*$/;

this is  i am using only for 192.168.1.0 upto here how can i allow ' /' slash as well.

 

Thanks,

Kumar

Tera Patron
Tera Patron

Hi,

please try to use valid regex for this

Regards
Ankur

Tera Contributor

HI ANkur,

how can we exclude '/ ' in regex? can you please help me here

Thanks,

kumrar

Mega Contributor

Hi Ankur, 

Can you please help, how to compare Ipv6 addresses.

Thanks,

sridevi

Tera Patron
Tera Patron

Hi,

you need to use the regex for Ipv6

sharing link here

https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses

Regards
Ankur

Mega Contributor

Hello All,

 

Could you please help with the script to use in above code for validating subnet included in IP.

eg: 10.148.96.0/2

It should accept the subnet as well like 10.148.96.0/24 or this /24 or any other subnets should be accepted.
 
thanks,
Gauri

 

Kilo Contributor

Hi Ankur,

How we can validate multiple IP address entered in a Single line text variable when the IP address is entered with comma or space?

example : xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx

Regards,

Priya

Tera Patron
Tera Patron

Hi Priya,

Currently it would only validate single value. for your case you will have to enhance it

Regards
Ankur

Tera Contributor

Ankur,

As always, thank you for your helpful posts. I am wondering, how did you find the code the field is using? I want to understand where or how this code is called but I am not able to figure when I look at this field type. i.e. Where in ServiceNow would I find the code you're referencing in your post? 

 

Thank you

Tera Patron
Tera Patron

@bvloch 

you can use this on change of the field on which you need validation to run