Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Validate IP Address format

Ankita9793
Tera Contributor

Hi,

Can someone please help me with IP address validation Regex, maximum length allowed is 30, should not accept special characters including( "/" and "-" ). Should allow '" , " to enter multiple IPs.

Valid IP address formats example 

Single IP --> 10.1.1.10
Multiple IPs --> 10.0.0.10,10.0.0.11,10.0.0.12

 
6 REPLIES 6

Anurag Tripathi
Mega Patron
Mega Patron

Check this out

Validate IP Address Using Regex : A Great use of... - ServiceNow Community

 

You can put the multiple IPs in an array and validate it as shown in the Blog by Gunjan

-Anurag

 did you try this?

-Anurag

Hi Anurag,
Thanks for responding!!

But it is not working as expected, I am trying to achieve it through client script but my client script does not allow ',' in case of multiple IP addresses

Valid IP address formats are as below... 
Single IP --> 10.1.1.10
Multiple IPs --> 10.0.0.10,10.0.0.11,10.0.0.12

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ipAddresses = g_form.getValue('enter_ip_addresses')
    var ipRegex = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;
    if (!ipRegex.test(ipAddresses)) {
        g_form.clearValue('enter_ip_addresses');
        g_form.triggerErrorChangeFieldSet('No spaces or special characters are allowed''enter_ip_addresses');
    }
    if (newValue.length > 30) {
        g_form.clearValue('enter_ip_addresses');
        g_form.showFieldMsg('enter_ip_addresses''Allowed maximum length is 30 characters''error');
    }
}

 

Try this

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ipAddresses = g_form.getValue('enter_ip_addresses')
var ipList = ipAddresses.split(',');
    var ipRegex = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;
 

for (var i = 0; i < ipList.length; i++) {
    var ip = ipList[i];
    if (ipRegEx.test(ip)) {
        gs.info("Valid IP address: " + ip);
    } else {
        gs.warn("Invalid IP address: " + ip); // You can clear field and throw error here if neeed.
    }
}

  
}
-Anurag