- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2024 05:51 AM
Hi,
Regular expression to check below points
1)Field validation to check IP address should not be more than 3 digits if user trying to enter IP range max 255 it should throw an error. ex(255.255.255.255)
2)field should also check, if user try to enter IP address along with slash number which should not exceed more than 32. ex( 10.111.11.111/11) (after slash, number should not be more than 32)
Kindly help!
Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2024 07:20 AM
Hi @Nandini DM ,
Write an onChange Client Script on the field.
Assuming field name as - ip_address, below script is written. Tried on PDI and is working as expected.
Change ip_address with actual field name and check
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var ipRegex = new RegExp('^(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 regex = new RegExp('^(?:[0-9]|[1-2][0-9]|3[0-2])$');
var isValidIp = '';
var isSubnet = '';
isValidIp = ipRegex.test(newValue);
var splitip = '';
var ipCheck = '';
var subnetCheck = '';
if (newValue.indexOf('/') != -1) {
splitip = newValue.split('/');
ipCheck = splitip[0];
subnetCheck = splitip[1];
isValidIp = ipRegex.test(ipCheck);
isSubnet = regex.test(subnetCheck);
}
if (!isValidIp) {
g_form.showFieldMsg('ip_address', 'Invalid IP', 'error');
} else if(!isSubnet && subnetCheck != ''){
g_form.showFieldMsg('ip_address', 'Invalid Subnet', 'error');
}
}
Mark the response correct and helpful if the answer assisted your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2024 09:03 AM
You could try checking the OOB regex for IP addresses: https://YOURINSTANCE.service-now.com/nav_to.do?uri=question_regex.do?sys_id=f2f52405530020107d13ddee...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2024 09:13 AM
Hi there,
Where do you want to apply this? Client side? Core UI or Portal?
Anyway, there is already out-of-the-box code for this which might be enough for your case already? The out-of-the-box regex:
((^\s*(((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]?))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2024 09:14 AM
If this is about portal, I wrote a few articles on this a 5 years ago:
- 2019-09-16 - Article - Regex Variable Validation on mandatory variables [workaround]
- 2019-08-14 - Article - Regexes for Catalog Items Variable Validation, part 2
- 2019-04-22 - Article - Service Portal Catalog Items: Regex Field Validation
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field