- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2022 09:24 PM
Hi Team,
We have below requirement to validate two IP address. I have wrote for one but couldn't think of including two validations:
Requirement:
the user should enter one or two IP addresses separated by a single space. Then the form should validate the one or two IP addresses. If both valid, the field keeps the values, otherwise if one or more is invalid, delete the invalid IPs and ask the user to enter the failed IP again.
Single IP address validation onChange client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (/^(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]?)$/.test(newValue)) {
return (true);
} else {
g_form.setValue('abc', ''); //where abc is the field for validating IP addresss
alert("'" + newValue + "' is not a valid IP address. Please enter a valid IP address.");
}
}
Please suggest the script for above requirement.
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
‎02-28-2022 10:00 PM
Following will validate 2 ip address. If one or both is invalid, it will delete the invalid address and display a message.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var pattern = /^(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 ipAddressList = newValue.split(' ');
var validIpList = [];
var errFlg = false;
for (var i = 0; i < ipAddressList.length; i++) {
if (pattern.test(ipAddressList[i])) {
validIpList.push(ipAddressList[i]);
} else {
errFlg = true;
}
}
if (errFlg) {
g_form.setValue('ip_address', validIpList.join(' '));
g_form.showFieldMsg('ip_address', 'There was an invalid ip address. Please re-enter.', 'error');
} else {
g_form.hideFieldMsg('ip_address');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2022 09:41 PM
Hi,
You could make use of the OOB Variable Validation Regex.
Here is a sample of what i created.
- Navigate to Service Catalog > Catalog Variables > Variable Validation Regex.
- Click New.
aIn my catalog variable item
select the regular expression created for IP Address.
Result:
Invalid result:
valid result:
Note I'm using Simple regex to check for an IP address:
^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
please see this regular expression link for reference.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2022 10:00 PM
Following will validate 2 ip address. If one or both is invalid, it will delete the invalid address and display a message.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var pattern = /^(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 ipAddressList = newValue.split(' ');
var validIpList = [];
var errFlg = false;
for (var i = 0; i < ipAddressList.length; i++) {
if (pattern.test(ipAddressList[i])) {
validIpList.push(ipAddressList[i]);
} else {
errFlg = true;
}
}
if (errFlg) {
g_form.setValue('ip_address', validIpList.join(' '));
g_form.showFieldMsg('ip_address', 'There was an invalid ip address. Please re-enter.', 'error');
} else {
g_form.hideFieldMsg('ip_address');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2022 10:02 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-10-2022 04:49 AM
Hi Hitoshi Ozawa,
Can we separate IP address with comma?