URL validation for a request form

Anna L
Tera Contributor

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?

1 ACCEPTED SOLUTION

Manik
ServiceNow Employee
ServiceNow Employee

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;
}


View solution in original post

11 REPLIES 11

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


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


Hi Ronald,



After this line var g=g_form.getValue('field name'); put alert statement and check once


i.e alert(g);


Deepak Ingale1
Mega Sage

You can store the valid URLs in another table and check if user is entering any of the URL listed from valid URL.


Chuck Tomasi
Tera Patron

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