Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

@Anusha dande 

Did you get a chance to check on the regex I shared.

Please test using that

For that the value you gave won't match -> https://pepsi

find_real_file.png

Regards
Ankur

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

Hi Ankur,

I have tried witj your code. But it is giving invalid URL even i am giving "https://pepsicodev.service-now.com"  as it is correct Url

Please give me suggestions to achive this 

 

Regards

Anusha

 

Hi,

when I used regex I shared and your value it matched the regular expression so the Regex is correct.

please share your script

find_real_file.png

Regards
Ankur

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

Hi Ankur ,

 

Please see attched . It is giving invalid url 

 

 

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

 

 

Regards

Anusha

Hi,

So that variable/field is of html type

So update as this

var url = g_form.getValue('u_link_to_training_materials');

var res = url.toString().match(/(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/g);

if (res == null)) {
alert("invalid url");

g_form.disableAttachments();
    return false;
}

Regards
Ankur

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