- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2022 01:14 AM
I had added an URL field in form where users will give URL values as input ; and I want to validate that URL field that means if user enters URL consisting (https / www) it will take the value OR ELSE if user enters string or integer value in place of URL field ,it will show alert message to the USER.
How can I fix this?
Please help.
Regards,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2022 01:52 AM
Hi,
you can use onChange client script + Regex for this
OR
you can also use onSubmit client script + Regex
Example onSubmit script
function onSubmit() {
var url = g_form.getValue('url_field');
var validateURL = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/.test(url);
if (!validateURL) {
g_form.addErrorMessage(getMessage("Enter a valid URL"));
return false;
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2022 01:21 AM
Hi,
Add an onChange Client script to the field.
https://docs.servicenow.com/bundle/sandiego-application-development/page/script/client-scripts/concept/client-scripts.html
In that script use a regex to match what you will allow and display a message if format incorrect.
script _could_ be something like:
var re = <Your regex>;
if (!re.test(newValue)) {
alert("invalid url");
return false;
}
hth,matt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2022 01:53 AM
Hi again,
I always think its work looking at anything that ships OOB.
I was just looking around client scripts and found on for the ldap_server_url table, it's called 'Validate Server URL for XSS' - not sure if you'll have that table. If not script is:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (newValue.indexOf(">") > -1 || newValue.indexOf("<") > -1) {
var msg = getMessage("Characters < and > are not allowed");
g_form.showFieldMsg(control, msg, "error");
} else {
g_form.hideFieldMsg(control);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2022 01:52 AM
Hi,
you can use onChange client script + Regex for this
OR
you can also use onSubmit client script + Regex
Example onSubmit script
function onSubmit() {
var url = g_form.getValue('url_field');
var validateURL = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/.test(url);
if (!validateURL) {
g_form.addErrorMessage(getMessage("Enter a valid URL"));
return false;
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2022 03:48 AM
Hi Ankur,
This is working. Thanks for the suggestion.
Regards,
Abhradipa