Using JavaScript Regular expressions to verify the URL format is correct, a lot of ways, this is only a way to achieve.
The code is as follows:
<script type= "Text/javascript" >
function Isurl (str) {
var regurl =/(http\:\/\/)? [\w.] +) (\/[\w-\.\/\?%&=]*)/gi;
var result = Str.match (Regurl);
if (result!=null) {alert ("Yes")}
else {alert ("no!")
}} </script>
<script type= "text/javascript" >
function Isurl (str) {
var regurl =/(http\:\/\/)? [\w.] +) (\/[\w-\.\/\?%&=]*)/gi;
var result = Str.match (Regurl);
if (result!=null) {alert ("Yes")}
Else{alert ("no!")
}} </script> |
Call Method:
<form name= "Bao" >
<input type= "text" name= "TXT" >
<input type=button value= "Testurl" oncli ck= "Isurl (document.bao.txt.value)" >
</FORM> |
Explain:
(http\:\/\/)? Represents a match http://0 or 1 times,
([\w.] +) Matching www.sian.com
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
<script>
function Checkadd () {
var websitename=$ ("#websiteName"). Val () | | "";
var url=$ ("#url"). Val () | | "";
if (websitename== "") {
alert ("Please enter URL name");
return false;
}
var reg=/^ ([hh][tt]{2}[pp]:\/\/|[ hh][tt]{2}[pp][ss]:\/\/) (([a-za-z0-9-~]+) \.) + ([a-za-z0-9-~\/]) +$/;
if (url== "") {
alert ("Please enter URL");
return false;
}
if (!reg.test (URL)) {
alert ("Incorrect URL format");
return false;
}
}
</script>
Call
<s:submit value= "Add" onclick= "return Checkadd ()" ></s:submit>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////
Another one:
http://blog.csdn.net/vipxiaotian/article/details/1774686
////////////////////////////////////////////////////////////////////
function Checkurl (str) {
var regurl = new RegExp ();
Regurl.compile ("^[a-za-z]+://[a-za-z0-9-_]+\\.[ a-za-z0-9-_%&\?\/.=]+$ ");
if (! Regurl.test (str)) {return
false;
}
return true;
}
//////////////////////////////////////////////////////////////////////
More stringent testing methods
Http://www.phpernote.com/javascript-function/337.html