Regular Expressions for IP address

Nandini DM
Tera Contributor

Hi,

 

Regular expression to check below points 

1)Field validation to check IP address should not be more than 3 digits if user trying to enter IP range max 255 it should throw an error. ex(255.255.255.255)

2)field should also check, if user try to enter IP address along with slash number which should not exceed more than 32. ex( 10.111.11.111/11) (after slash, number should not be more than 32)

 

Kindly help!

 

Thanks.

 

1 ACCEPTED SOLUTION

Rupanjani
Giga Guru

Hi @Nandini DM ,

 

Write an onChange Client Script on the field.

Assuming field name as - ip_address, below script is written. Tried on PDI and is working as expected.

Change ip_address with actual field name and check

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

	var ipRegex = new RegExp('^(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]?)$');
	var regex = new RegExp('^(?:[0-9]|[1-2][0-9]|3[0-2])$');
     
    var isValidIp = '';
	var isSubnet = '';
	isValidIp = ipRegex.test(newValue);

	var splitip = '';
    var ipCheck = '';
    var subnetCheck = '';

	if (newValue.indexOf('/') != -1) {
        splitip = newValue.split('/');
        ipCheck = splitip[0];
        subnetCheck = splitip[1];
		isValidIp = ipRegex.test(ipCheck);
		isSubnet =  regex.test(subnetCheck);
    }	

    if (!isValidIp) {
        g_form.showFieldMsg('ip_address', 'Invalid IP', 'error');
    } else if(!isSubnet && subnetCheck != ''){
		g_form.showFieldMsg('ip_address', 'Invalid Subnet', 'error');
	} 
}

 


Mark the response correct and helpful if the answer assisted your question.

View solution in original post

12 REPLIES 12

@Rupanjani - Along with the two condition , it should also check for URL like "www.google.com" ,Sample : "255.255.255.255 | 255.222.222.222/12 | www.google.com"

 

 

@Nandini DM ,

 

Check the below link:

https://www.servicenow.com/community/itsm-forum/how-to-provide-validation-for-input-url-field-either...

 

Try to replace the script with the below and test:

 

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

	var ipRegex = new RegExp('^(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]?)$');
	var regex = new RegExp('^(?:[0-9]|[1-2][0-9]|3[0-2])$');
        var validateURL = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/.test(newValue);

     
        var isValidIp = '';
	var isSubnet = '';
	isValidIp = ipRegex.test(newValue);

	var splitip = '';
        var ipCheck = '';
        var subnetCheck = '';

	if (newValue.indexOf('/') != -1) {
        splitip = newValue.split('/');
        ipCheck = splitip[0];
        subnetCheck = splitip[1];
		isValidIp = ipRegex.test(ipCheck);
		isSubnet =  regex.test(subnetCheck);
    }	

    if (!isValidIp) {
        g_form.showFieldMsg('ip_address', 'Invalid IP', 'error');
    } else if(!isSubnet && subnetCheck != ''){
		g_form.showFieldMsg('ip_address', 'Invalid Subnet', 'error');
	} else if (!validateURL) {
		g_form.showFieldMsg('ip_address', 'Enter Valid Ip or Valid URL', 'error');
	}
}

 


Mark the response correct and helpful if the answer assisted your question.

Hi @Rupanjani 

That URL part is showing below message on everything I enter , I tried giving (222.222.222.222)-->Invalid , (222.222.222.222/12)--->enter valid ip or valid URL and (www.aa.com)-->enter valid ip or valid URL

Enter Valid Ip or Valid URL

 

@Rupanjani 
Validate URL is not working as expected and how to make IP address comma separated .

@Nandini DM 

Make the variable type 'Single Line Text' or 'Multi Line Text', depending on how many IP addresses will be mentioned in comma separated.

 

In order to make validations on the comma separated we need to modify the above code, by adding code for splitting the text with comma first and then applying while loop to check every IP address is a valid one or not.


Mark the response correct and helpful if the answer assisted your question.