Amod Vardhan Amod Vardhan - 1 year ago 111
jQuery Question

url validation jquery using regex

I want to validate URL field something like below -




First two are validation using below regex -

function is_valid_url(url) {
return /^http(s)?:\/\/(www\.)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/.test(url);

but, when i am putting scenario 3 in textbox it showing me invalid URL.

Any help will be highly appreciated.

Thanks in Advance


Answer Source

Try this, It worked for me.

var url = $("#<%= txtUrl.ClientID %>").val();
var pattern = /^(http|https)?:\/\/[a-zA-Z0-9-\.]+\.[a-z]{2,4}/;

args.IsValid = pattern.test(url);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download