Introduction:
Here I will explain regular expression for validating website url with or without http in JQuery, JavaScript or regex to validate website url with or without http(s) in JQuery, JavaScript
Description:
In previous articles I explained validate email address using JQuery, how to disable or enable all the controls on page using JQuery , validate phone number using JQuery and many articles relating to JQuery, JavaScript. Now I will explain regular expression for validating url with or without http in JQuery, JavaScript.
In previous articles I explained validate email address using JQuery, how to disable or enable all the controls on page using JQuery , validate phone number using JQuery and many articles relating to JQuery, JavaScript. Now I will explain regular expression for validating url with or without http in JQuery, JavaScript.
Regular
expression to validate url with or without http
$('#btnValidate').click(function() {
var txt = $('#txturl').val();
var re = /(http(s)?:\\)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?/
if (re.test(txt)) {
alert('Valid URL')
}
|
If you want to see it in complete
example need to write the code like as shown below
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Regex for urls with or without http Page</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function()
{
$('#btnValidate').click(function() {
var txt = $('#txturl').val();
var re = /(http(s)?:\\)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?/
if (re.test(txt)) {
alert('Valid URL')
}
else {
alert('Please
Enter Valid URL');
return false;
}
})
})
</script>
</head>
<body>
<div>
Enter URL :<input type="text" id="txturl" />
<input type="button" id="btnValidate" value="Validate" />
</div>
</body>
</html>
|
Live Demo
Enter URL : |
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
10 comments :
IT WASTE APPLICATION
IT NOT CORRECT VALIDATE
It's not working correctly...
check with this - ww.dfg
Fails for : http://www.com
The regular expression identifies tk ..... as I do. ?
\.((tk|com|ru|ora|net|eu|mobi)(?:(\/)?)(?:\S*))$
http://it.wikipedia.org/wiki/.tk/#Particolarit.C3.A0
thanks
If i enter "www.test." it shows valid URL
Not working properly ???????????????
Fail to test www.akbible.
Apparently gmail.co.uk.no.never.bigfatthing.banana is a valid url?
asdfas.asdfasdfasdf.sd is a valid url
http://http://googl.com is this valid url?
Note: Only a member of this blog may post a comment.