URL Validation

Arun87
Giga Contributor

Hi All,

I have created a  single line variable field for catalog item. Now i want to validate the url. Kindly help on this how to achieve this. Thanks in advance 

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi,

I have modified the script of willem slightly to exclude www. Check this.

var url=newValue;
var re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
if(url.indexOf("www.") > -1 || !re.test(url)) {
    g_form.showFieldMsg('your field', 'invalid url');
    return false;
}

View solution in original post

17 REPLIES 17

Jyoti8
Kilo Guru
Hi Arun,


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




I hope it will help you.


Please mark helpful and Correct if it helps.

Thanks..!

Arun87
Giga Contributor

Hi Jyoti,

It is not working

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Arun,

please check below links for help

URL variable validation not working in Service Portal as it does in DesktopUI

URL validation

URL validation for a request form

Regards
Ankur

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

Willem
Giga Sage
Giga Sage

You can create onChange client script on the URL field to validate:

replace your field with the url field

var url = g_form.getValue('your 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)) {
    g_form.showFieldMsg('your field', 'invalid url');
    return false;
}