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

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


Volker1
Kilo Contributor

PLEASE, DO NOT USE THE ACCEPTED SOLUTION, IT IS NOT WORKING CORRECTLY.

Example:
the mentioned RegEx validates to true using this URL: httpsss.safdgoiuhasdg

httpsss.safdgoiuhasdg is not a valid URL.