I’ve a basic URL validation in my appliction. Right now i’m using the following code.
//validates whether the given value is
//a valid URL
function validateUrl(value)
{
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(value);
}
But right now it is not accepting URLs without the protocol. For ex. if i provide http://www.google.com it is not accepting it. How can i modify the RegEx to make it accept URLs without protocol?
Here’s a big long regex for matching a URL:
The expanded version of that (to help make it understandable):
These both come from this page, but modified slightly to make protocol properly optional – you should read that page to help understand what it’s doing, and it also has a variant which only matched web-based URLs, which you may want to take a look at too.