Regex Validation for IP address || CIDR || URL
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2024 08:00 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2024 04:45 AM
Hi @Community Alums
In the below expression:
new RegExp('^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/([1-9]|[1-2][0-9]|3[0-2]))?|((www\\.)?((?:(?!www\\.|\\.).)+\\.[a-zA-Z0-9]+)))(\\s*,\\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/([1-9]|[1-2][0-9]|3[0-2]))?|((www\\.)?((?:(?!www\\.|\\.).)+\\.[a-zA-Z0-9]+))))*$', 'gm');
How to make below URL to be Valid?
it should be valid for below scenarios:
www.sample.com --> Valid
Sample.sample.com --> Valid (without www, it should allow 2 dots)
Sample.com -->Valid
How to change above expression to change URL only by keeping everything as is.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2024 02:49 PM
Hi @Nandini DM ,
Here is the updated regex-
var ipv4Pattern = '((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})(\\/([0-9]|[12][0-9]|3[0-2]))?';
var urlPattern = '((www\\.)?([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,})';
var combinedPattern = new RegExp('^(' + ipv4Pattern + '|' + urlPattern + ')(\\s*,\\s*(' + ipv4Pattern + '|' + urlPattern + '))*$', 'gm');
var testStrings = [
'1.1.1.1',
'11.11.11.11/32',
'11.22.33.44,11.22.33.44/12',
'www.example.com',
'sample.example.com',
'example.com',
'11.22.33.44,www.example.com,11.22.33.44/12'
];
function validateInput(input) {
combinedPattern.lastIndex = 0;
return combinedPattern.test(input);
}
function runValidation() {
gs.print('Starting validation...');
for (var i = 0; i < testStrings.length; i++) {
var test = testStrings[i];
var result = validateInput(test);
gs.print('Validation result for "' + test + '": ' + result);
}
gs.print('Validation completed.');
}
runValidation();
Output-
*** Script: Starting validation...
*** Script: Validation result for "1.1.1.1": true
*** Script: Validation result for "11.11.11.11/32": true
*** Script: Validation result for "11.22.33.44,11.22.33.44/12": true
*** Script: Validation result for "www.example.com": true
*** Script: Validation result for "sample.example.com": true
*** Script: Validation result for "example.com": true
*** Script: Validation result for "11.22.33.44,www.example.com,11.22.33.44/12": true
*** Script: Validation completed.
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks & Regards,
Sanjay Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 12:06 AM
Hi @Community Alums
Everything is working except for below condition:
Validation result for "*.example.com" is not working, URL should also accept "*" in the initial point.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 06:13 AM
Hi @Nandini DM ,
Here you go-
// Background script for validating IPv4 addresses, CIDR notations, and URLs
var ipv4Pattern = '((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})(\\/([0-9]|[12][0-9]|3[0-2]))?';
var urlPattern = '((\\*\\.)?([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,})';
var combinedPattern = new RegExp('^(' + ipv4Pattern + '|' + urlPattern + ')(\\s*,\\s*(' + ipv4Pattern + '|' + urlPattern + '))*$', 'gm');
var testStrings = [
'1.1.1.1',
'11.11.11.11/32',
'11.22.33.44,11.22.33.44/12',
'www.example.com',
'sample.example.com',
'example.com',
'*.example.com',
'sanjay.servicenow.example.com',
'11.22.33.44,www.example.com,11.22.33.44/12'
];
function validateInput(input) {
combinedPattern.lastIndex = 0;
return combinedPattern.test(input);
}
function runValidation() {
gs.print('Starting validation...');
for (var i = 0; i < testStrings.length; i++) {
var test = testStrings[i];
var result = validateInput(test);
gs.print('Validation result for "' + test + '": ' + result);
}
gs.print('Validation completed.');
}
runValidation();
Output-
*** Script: Starting validation...
*** Script: Validation result for "1.1.1.1": true
*** Script: Validation result for "11.11.11.11/32": true
*** Script: Validation result for "11.22.33.44,11.22.33.44/12": true
*** Script: Validation result for "www.example.com": true
*** Script: Validation result for "sample.example.com": true
*** Script: Validation result for "example.com": true
*** Script: Validation result for "*.example.com": true
*** Script: Validation result for "sanjay.servicenow.example.com": true
*** Script: Validation result for "11.22.33.44,www.example.com,11.22.33.44/12": true
*** Script: Validation completed.
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks & Regards,
Sanjay Kumar