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

Steven Young
Tera Guru

What was your initial thought on validation?


Like is it a live website?



or is it on a list, that you have valid URL's stored in a table?


general validation like to avoid user entering "mysite" instead of proper terminology as "www.mysite.com". To your second question, there is no list of valid urls anywhere.


not the best idea, but a very simple idea would be and indexOf("www.") > -1    


that will ensure that it at least has a www. in it.  


Many URL's don't have www, especially internal sites.