【JavaScript】URL格式檢查

判斷全面性URL格式檢查,是個非常困難的一件事情,主要格式變化多種,爬了不少文以下是從某網站參考過來的,但也符合大多數URL格式,僅供參考,不能符合百分百URL格式。

<script language="JavaScript">
<!--
function IsURL(str_url){
   var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
   + "?(([0-9a-zA-Z_!~*'().&=+$%-]+: )?[0-9a-zA-Z_!~*'().&=+$%-]+@)?" //ftp的user@
   + "(([0-9]{1,3}.){3}[0-9]{1,3}" // IP URL- 123.123.123.123
   + "|" // allow IP和DOMAIN
   + "([0-9a-zA-Z_!~*'()-]+.)*" // DOMAIN- www.
   + "([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]." // second DOMAIN
   + "[a-z]{2,6})" // first level domain- .com or .museum
   + "(:[0-9]{1,4})?" // port- :80
   + "((/?)|" // a slash isn't required if there is no file name
   + "(/[0-9a-zA-Z_!~*'().;?:@&=+$,%#-]+)+/?)$";
   var re=new RegExp(strRegex);
   //re.test()
   if (re.test(str_url)){
      return (true);        //符合
   }else{
      return (false);      //不符合
   }
}

function check(){
 alert(IsURL(document.form1.urls.value));
}
-->
</script>

部份HTML︰
<form id="form1" name="form1" method="post" action="">
  <input name="urls" type="text" />
  <input type="button" name="Submit" value="檢查"  onclick="check();"/>
</form>
 
執行畫面︰



 

 

留言

這個網誌中的熱門文章

【ASP】日期轉換(西元<-->民國)

【VB】使用NPOI元件來匯出Excel--DataTableToExcel

【SQL】符號切割字串變成多欄