Validation of URL

dande Anusha
Tera Contributor

Hi ,

 

we got requirement to check URL validation for one field.

I have written the below script , but it is always giving invalid URL (i am giving "https://development.service-now.com " for testing)

 

var url = g_form.getValue('u_link_to_training_materials');
//var re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
var re = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/ ;
if (!re.test(url)) {
alert("invalid url");
g_form.disableAttachments();
return false;
}

 

Please help me on this if anyone is having an idea on this type of requirement

 

Regards

Anusha

1 ACCEPTED SOLUTION

@Anusha dande 

Thank you for marking my response as helpful.

Let me know if I have answered your question.

If so, please mark my response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

13 REPLIES 13

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

You may find below thread helpful.

URL validation for a request form

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

sharing the script from the link I referred to

URL validation for a request form

var url = g_form.getValue('u_link_to_training_materials');
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");

g_form.disableAttachments();
    return false;
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Pranesh072
Mega Sage
Mega Sage

You can use following regex - recognizing URLs in most used formats such as:

  • www.google.com
  • http://www.google.com
  • mailto:somebody@google.com
  • somebody@google.com
  • www.url-with-querystring.com/?url=has-querystring




var url = g_form.getValue('u_link_to_training_materials');
var re=/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/;
if (!re.test(url)) {
	alert("invalid url");
	g_form.disableAttachments();
	return false;
}

 

Hi Pranesh,

 

Thanks for the answer. But it is accepting the "https://pepsi ",even with out ".com"

Could you please help me with any other solution which should not accept the url with out  ".com"

 

Thanks&Regards

Anusha