Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Regular Expression(Regex) to Validate URL without http(s) in JavaScript

Sep 14, 2013
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 JQueryJavaScript

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.

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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

6 comments :

Anonymous said...

IT WASTE APPLICATION

Anonymous said...

IT NOT CORRECT VALIDATE

Anonymous said...

It's not working correctly...
check with this - ww.dfg

Mallikarjun said...

Fails for : http://www.com

Anonymous said...

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

Anonymous said...

If i enter "www.test." it shows valid URL

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.