- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2022 10:06 AM
So I have a requirement to allow URLs, No spaces and comma delimited. A dash "-" is allowed just not at beginning or beside a period "." And lastly * is only available in the first portion of the URL. I have create a regular expression that passes via regex101, but in my ServiceNow instance it fails.
Pass would be:
www.test.com
www.example.com,www-01.user.com,www.user-test.com,*.example.com
*.example.com,test.user.com
Fail:
www*.example.com
-www.example.com
www-.example.com
www.-example.com
Here is my RegEx:
^(([a-zA-Z0-9-]{1,}[a-zA-Z0-9]{1,150}\.[a-zA-Z0-9]{1,150}[a-zA-Z0-9-]{1,150}\.[a-zA-Z0-9]{1,150})|^([*]{1,1}\.[a-zA-Z0-9]{1,150}[a-zA-Z0-9-]{1,150}\.[a-zA-Z0-9]{1,150}))|((\,[a-zA-Z0-9-]{1,}[a-zA-Z0-9]{1,150}\.[a-zA-Z0-9]{1,150}[a-zA-Z0-9-]{1,150}\.[a-zA-Z0-9]{1,150})|(\,[*]{1,1}\.[a-zA-Z0-9]{1,150}[a-zA-Z0-9-]{1,150}\.[a-zA-Z0-9]{1,150}))
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2022 04:08 AM
I got the Client script version to work, the user can enter values in a single field and the array works perfect. The script you posted the test line was backwards.
if (!urlArray[x].test(re))
should actually be (!re.test(urlArray[x])) and then it worked perfectly. Also I mistakenly had a return true from the client script Yousaf provided below. I also got the - to work in beginning and not near a . and I got only the since * to work. Here is my final solution.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var urlArray = [];
var urlField = g_form.getValue('provide_url');
urlArray = urlField.split(',');
// This will restrict the usages of certain special characters.
var re = /^(([a-zA-Z0-9-]{1,}[a-zA-Z0-9]{1,}\.[a-zA-Z0-9]{1,}[a-zA-Z0-9-]{1,150}\.[a-zA-Z0-9]{1,})|^([*]{1,1}\.[a-zA-Z0-9]{1,}[a-zA-Z0-9-]{1,}\.[a-zA-Z0-9]{1,}))$/;
for (var x = 0; x < urlArray.length; x++) {
if (!re.test(urlArray[x])) {
g_form.showFieldMsg('provide_url','Invalid URL entry, please exclude slashes, colons and spaces.','error');
return false;
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2022 05:27 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2022 04:08 AM
I got the Client script version to work, the user can enter values in a single field and the array works perfect. The script you posted the test line was backwards.
if (!urlArray[x].test(re))
should actually be (!re.test(urlArray[x])) and then it worked perfectly. Also I mistakenly had a return true from the client script Yousaf provided below. I also got the - to work in beginning and not near a . and I got only the since * to work. Here is my final solution.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var urlArray = [];
var urlField = g_form.getValue('provide_url');
urlArray = urlField.split(',');
// This will restrict the usages of certain special characters.
var re = /^(([a-zA-Z0-9-]{1,}[a-zA-Z0-9]{1,}\.[a-zA-Z0-9]{1,}[a-zA-Z0-9-]{1,150}\.[a-zA-Z0-9]{1,})|^([*]{1,1}\.[a-zA-Z0-9]{1,}[a-zA-Z0-9-]{1,}\.[a-zA-Z0-9]{1,}))$/;
for (var x = 0; x < urlArray.length; x++) {
if (!re.test(urlArray[x])) {
g_form.showFieldMsg('provide_url','Invalid URL entry, please exclude slashes, colons and spaces.','error');
return false;
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2022 10:50 AM
Hi Brian,
I guess its working please check and let me know just removed -
^(([a-zA-Z0-9]{1,}[a-zA-Z0-9]{1,150}\.[a-zA-Z0-9]{1,150}[a-zA-Z0-9-]{1,150}\.[a-zA-Z0-9]{1,150})|^([*]{1,1}\.[a-zA-Z0-9]{1,150}[a-zA-Z0-9-]{1,150}\.[a-zA-Z0-9]{1,150}))|((\,[a-zA-Z0-9-]{1,}[a-zA-Z0-9]{1,150}\.[a-zA-Z0-9]{1,150}[a-zA-Z0-9-]{1,150}\.[a-zA-Z0-9]{1,150})|(\,[*]{1,1}\.[a-zA-Z0-9]{1,150}[a-zA-Z0-9-]{1,150}\.[a-zA-Z0-9]{1,150}))
***Mark Correct or Helpful if it helps.***
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2022 11:45 AM
www.user.com,www-www.text.com,www.w-user.com,*.example.com
Came back as invalid for your string as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2022 12:11 PM
