Categorized | Javascript

How can you validate a url

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
}

Leave a Reply


  • Popular
  • Latest
  • Comments
  • Tags
  • Subscribe

43,837
Unique
Visitors
Powered By Google Analytics