- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2022 11:13 PM
HI Team,
We have a requirement to validate a text field within the specified range from the other field in the form. Fr ex if the field "a" has a IP range of "192.168.1.0/24" , the other field "b" should be between 0-24 with a valid IP address. Ex: between 192.168.1.1 to 102.168.1.24
Please help in the script.
Thanks in advance,
Abhishek
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 02:02 AM
So it's not a CIDR.
It'll be much easier.
Create a regex like below for field "a"
^([01]?\d\d?|2[0-4]\d|25[0-5])(?:\.[01]?\d\d?|\.2[0-4]\d|\.25[0-5]){3}(\/([01]?\d\d?|2[0-4]\d|25[0-5]))$
Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
try {
var mask = g_form.getValue('a').split('/');
var end = mask[1];
var maskOctet = mask[0].split('.');
var endOctet = newValue.split('.');
for (var i=0; i<3; i++) {
if (maskOctet[i] != endOctet[i]) {
g_form.showFieldMsg('b', 'invalid ip address.', 'error');
return;
}
}
if (endOctet[3] < 1 || endOctet[3] > end) {
g_form.showFieldMsg('b', 'invalid ip address.', 'error');
}
} catch (e) {
alert(e.message);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 12:11 AM
Hi Abhishek,
I got the basic idea but is the question correct? "/24" implies subnet mask 255.255.255.0. This implies valid ip address range from 192.168.1.0/24 to 192.168.1.1 0 192.168.1.254.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 01:20 AM
Here is the script that will validate ip address with CIDR.
- Since IP address and IP address with CIDR may be used often, created following Regex. (Service Catalog > Catalog Variables > Variable Validation Regex)
- IPv4
^(?:(?: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]?)$
- IPv4 with subnet mask
^([01]?\d\d?|2[0-4]\d|25[0-5])(?:\.[01]?\d\d?|\.2[0-4]\d|\.25[0-5]){3}(\/[0-2]\d|\/3[0-2])$
- Define fields "a" and "b". Set Validation Regex on field "a" to "IPv4 with subnet mask" and field "b" to "IPv4"
- Define Client Script on field "b"
function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; } var range = getIpRangeFromAddressAndNetmask(g_form.getValue('a')); var start = range[0]; var end = range[1]; if (newValue > end || newValue < start) { g_form.showFieldMsg('b', 'ip address is not permitted by subnet mask.', 'error'); } function getIpRangeFromAddressAndNetmask(str) { var part = str.split("/"); // part[0] = base address, part[1] = netmask var ipaddress = part[0].split('.'); var netmaskblocks = ["0", "0", "0", "0"]; if (!/\d+\.\d+\.\d+\.\d+/.test(part[1])) { // part[1] has to be between 0 and 32 netmaskblocks = ("1".repeat(parseInt(part[1], 10)) + "0".repeat(32 - parseInt(part[1], 10))).match(/.{1,8}/g); netmaskblocks = netmaskblocks.map(function(el) { return parseInt(el, 2); }); } else { // xxx.xxx.xxx.xxx netmaskblocks = part[1].split('.').map(function(el) { return parseInt(el, 10); }); } // invert for creating broadcast address (highest address) var invertedNetmaskblocks = netmaskblocks.map(function(el) { return el ^ 255; }); var baseAddress = ipaddress.map(function(block, idx) { return block & netmaskblocks[idx]; }); var broadcastaddress = baseAddress.map(function(block, idx) { return block | invertedNetmaskblocks[idx]; }); if (baseAddress[3] == 0) { baseAddress[3] = 1; } if (broadcastaddress[3] == 255) { broadcastaddress[3] = 254; } return [baseAddress.join('.'), broadcastaddress.join('.')]; } }
Execution:
Case 1: Valid IP address
Case 2: Invalid IP address
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 01:38 AM
Hi
"/24" is not a subnet mask. it means "0-24" range at the end of IP like 192.168.1.1 - 192.168.1.24. Anything above 24 will be error like 192.168.1.25.
Could you please remodify the script and update please?
Thanks,
Abhishek

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2022 02:02 AM
So it's not a CIDR.
It'll be much easier.
Create a regex like below for field "a"
^([01]?\d\d?|2[0-4]\d|25[0-5])(?:\.[01]?\d\d?|\.2[0-4]\d|\.25[0-5]){3}(\/([01]?\d\d?|2[0-4]\d|25[0-5]))$
Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
try {
var mask = g_form.getValue('a').split('/');
var end = mask[1];
var maskOctet = mask[0].split('.');
var endOctet = newValue.split('.');
for (var i=0; i<3; i++) {
if (maskOctet[i] != endOctet[i]) {
g_form.showFieldMsg('b', 'invalid ip address.', 'error');
return;
}
}
if (endOctet[3] < 1 || endOctet[3] > end) {
g_form.showFieldMsg('b', 'invalid ip address.', 'error');
}
} catch (e) {
alert(e.message);
}
}