
How to validate URL in regex Javascript

<html>
<head>
<title>Url validation using regex</title>
<script type="text/javascript">
function validate() {
var url = document.getElementById("url").value;
var pattern = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
if (pattern.test(url)) {
alert("Url is valid");
return true;
}
alert("Url is not valid!");
return false;
}
</script>
</head>
<body>
<h2>Validating Url..</h2>
Url :
<input type="text" name="url" id="url" />
<input type="submit" value="Check" onclick="validate();" />
</body>
</html>

url pattern is like HYPERLINK "http://www.roseindia.net/hibernate/examples/criteria" http://www.roseindia.net/hibernate/examples/criteria
In pattern = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!-\/]))?/;
/../(forward slashes) is used to quote your regular expression.
() is used to group the content.
| is used for or condition
\/ backslash consider / as a literal .
\w+ for one or more alphanumeric value.
{0,1} shows the range of content having length between 0 to 1.
* matches the preceding character zero or more time.
[..] matches any letter enclosed with in.
+ shows that preceding character come 1 or more times.
. Shows that \ will treat . as a literal.
$ is used for ending the string.
test() method takes one argument and check that to the pattern.