How can you validate a url

Posted In Javascript - By admin On Tuesday, July 28th, 2009 With 0 Comments

There are many examples of client side validation of URL fields. The fastest and best way to validate a field is by use of JavaScript’s regular expression object. The hard part is coming up with a good regular expression to validate a URL.


function iVali(YourUrl)
{
var regUrl = /((ht|f)tp(s?)\:\/\/|~\/|\/)?(\w+:\w+@)?(([-\w]+\.)+(com|co.in|org|net|in|[a-z]{2}))(:[\d]{1,5})?(((\/([-\w~!$+|.,=]|%[a-f\d]{2})+)+|\/)+|\?|#)?((\?([-\w~!$+|.,*:]|%[a-f\d{2}])+=([-\w~!$+|.,*:=]|%[a-f\d]{2})*)(&([-\w~!$+|.,*:]|%[a-f\d{2}])+=([-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(#([-\w~!$+|.,*:=]|%[a-f\d]{2})*)?/;

if (regUrl.test(YourUrl) == false)
return false
else
return true
}

About -

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>