regex/ script to validate IP's - Individual IPs, Multiple IPs, IP range and IP subnets.

Ankita9793
Tera Contributor

Hi All,

 

can someone please help me with the regex/ script to validate IP's. “Enter IP addresses” field should allow to enter Individual IPs, Multiple IPs, IP range and IP subnets.

 

example : 

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

IP range --> 10.0.0.10-10.0.0.100 

IP subnet --> 10.0.0.10/32

 

1 ACCEPTED SOLUTION

Bhavya11
Kilo Patron

hi @Ankita9793 ,

 

can you try below regex

 

function validateIPField(ipField) {
    // Define regex patterns for each valid IP format
    var singleIPRegex = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
    var multipleIPsRegex = /^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(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]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))*$/;
    var ipRangeRegex = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(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]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
    var ipSubnetRegex = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/([0-9]|[1-2][0-9]|3[0-2]))$/;

    // Check if the IP field matches any of the valid patterns
    return singleIPRegex.test(ipField) ||
           multipleIPsRegex.test(ipField) ||
           ipRangeRegex.test(ipField) ||
           ipSubnetRegex.test(ipField);
}

// Replace with your actual field value
var ipField = '10.1.1'; 

if (!validateIPField(ipField)) {  // Note the negation here (!)
    gs.addErrorMessage("Invalid IP address format in field: " + ipField);
}

 

Thanks,

BK

View solution in original post

1 REPLY 1

Bhavya11
Kilo Patron

hi @Ankita9793 ,

 

can you try below regex

 

function validateIPField(ipField) {
    // Define regex patterns for each valid IP format
    var singleIPRegex = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
    var multipleIPsRegex = /^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(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]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))*$/;
    var ipRangeRegex = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(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]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
    var ipSubnetRegex = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/([0-9]|[1-2][0-9]|3[0-2]))$/;

    // Check if the IP field matches any of the valid patterns
    return singleIPRegex.test(ipField) ||
           multipleIPsRegex.test(ipField) ||
           ipRangeRegex.test(ipField) ||
           ipSubnetRegex.test(ipField);
}

// Replace with your actual field value
var ipField = '10.1.1'; 

if (!validateIPField(ipField)) {  // Note the negation here (!)
    gs.addErrorMessage("Invalid IP address format in field: " + ipField);
}

 

Thanks,

BK