- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 12:45 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 01:19 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 01:19 AM
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