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:
Test Cases:
1- aspbucket
Result: Fail
2- aspbucket.com
Result: Pass
3-www.aspbucket.com
Result: Pass
4-http://aspbucket.com
Result:Pass
5-http://www.aspbucket.com
Result: Pass
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
Result: Pass
4-http://aspbucket.com
Result:Pass
5-http://www.aspbucket.com
Result: Pass
0 comments :
Post a Comment