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

Mark Manders
Mega Patron

This should get you started:

function validateIPAddress(ipAddress) {
    var regex = /^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(/([1-9]|[12][0-9]|3[0-2]))?$/;
    return regex.test(ipAddress);
}

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

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.

Hi @Rupanjani 
This is working all good , but if I need to add one more or condition to add URL like (facebook.com, google.com)
How should I adjust the code?

Hi @Nandini DM , 

 

Can you elaborate the URL condition, when it should be checked.

 

And if the above solution has helped you. Please mark it Helpful and mark it as solution.

 


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