- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2016 08:47 PM
Hi,
I have a client requirement to have a request form field "URL". Basically, asking users to enter a URL of the site they manage. Requirement is to validate the URL if it is valid. How would I do that considering I have validation criteria?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2016 11:44 PM
Hi Ronald,
You can use a regex to validate url.
var url = g_form.getValue('url_field');
var re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
if (!re.test(url)) {
alert("invalid url");
return false;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2016 09:23 PM
Hi Ronald, You can create a field of url and then create a client script for validating the url as below:
var g=g_form.getValue('field name');
if (g.startsWith('www') && g.endsWith('.com'))
{
alert('Validated');
}
else {
alert('Not validated');
}
Hope it helps...
Thanks,
Akhil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2016 11:18 PM
created a client script and referenced the catalog item and selected 'onsubmit' and used the code by changing the field name for url. didnt work

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2016 11:38 PM
Hi Ronald,
After this line var g=g_form.getValue('field name'); put alert statement and check once
i.e alert(g);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2016 09:15 PM
You can store the valid URLs in another table and check if user is entering any of the URL listed from valid URL.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2016 10:32 PM
Hi Ronald,
You could send an HTTP request to the URL and see if you get back the appropriate (200) response code.
GlideHttpRequest might help.
Table API Server-Side JavaScript Examples - ServiceNow Wiki