AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Saturday 25 June 2016

How to Validate URL without or with http(s) in JavaScript?

In this article I am going to discuss How to validate URL without or with http(s) in JavaScript? Please check below java script code.
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>How to Validate URL without or with http(s) in JavaScript?</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$( document ).ready(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>
 Input URL :<input type="text" id="txturl" />
<input type="button" id="btnValidate" value="Validate" />
</div>
</body>
</html>

Test Cases:
1- aspbucket
Result: Fail

2- aspbucket.com
Result: Pass

3-www.aspbucket.com
ResultPass

4-http://aspbucket.com
Result:Pass

5-http://www.aspbucket.com
ResultPass

0 comments :

Post a Comment

  • Popular Posts
  • Comments