<div class="codetitle"> <span><a style="CURSOR: pointer" data="11292" class="copybut" id="copybut11292" onclick="doCopy('code11292')"><u>複製程式碼</u></a></span> 程式碼如下:</div> <div class="codebody" id="code11292"> <br><script type="text/javascript"> <BR>//驗證一般網域校驗<BR>function checkNetURL(){ <BR>var netURL = document.getElementById("netURL"); <BR>//驗證一般的網域比如說www.baidu.com <BR>var regStr="^([a-zA-Z0-9-] \.){1,}(com|net|edu|miz| biz|cn|cc)$"; <BR>var re=new RegExp(regStr); //建立正規表示式物件<BR>//var re=/^([a-zA-Z0-9-] \ .){1,}(com|net|edu|miz|biz|cn|cc)$/; <BR>if(!re.exec(netURL.value)){ //驗證輸入的字串是否符合規則<BR>alert("您輸入的網站URL位址有誤!正確格式如mail.brookes.com"); <BR>netURL.focus(); <BR>return; <BR>} <BR>} <BR>//手機校驗<BR>function iphoneRegExp(){ <BR>var iphone= document.getElementById("iphone").value; <BR>var regStr="^((13[0-9])|( 14[0-9])|(15[^4,\D])|(18[0-9]))\d{8}$"; <BR>var re=new RegExp(regStr); <BR>if(!re.test(iphone)){ <BR>alert("規格錯誤,正確格式如152636363**(或以13,14,18開頭的手機號碼)"); <BR>} <BR> } <BR>//信箱校驗(還有點bug) <BR>/*function emailRegExp(){ <BR>var email= document.getElementById("email").value; <BR>var regStr="^( [a-zA-Z0-9u4E00-u9FA5]*[-_]?[a-zA-Z0-9]){1,} @([a-zA-Z0-9]*[-_]?[a -zA-Z0-9] ) [\.][A-Za-z]{2,3}([\.][A-Za-z]{2})?$"; <BR>var re= new RegExp(regStr); <BR>if(!re.test(email)){ <BR>alert("規格錯誤,正確格式如152636363**(或以13,14,18開頭的手機號碼)") ; <BR>//iphone.focus(); <BR>} <BR>}*/ <BR>//身分證校驗<BR>function cardRegExp(){ <BR>var card= document.getElementById(" card").value; <BR>var regStr="(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$) "; <BR>var re=new RegExp(regStr); <BR>if(!re.test(card)){ <BR>alert("輸入不合法,正確格式為:4101821897020356**"); <BR>} <BR>} <BR>//港澳省份證<BR>function gangAocardRegExp(){ <BR>var card= document.getElementById("card").value; <BR>var regStr="[A-Z]{ 1,2}[0-9]{6}[(]{1}[A-Z0-9]{1}[)]{1}"; <BR>var re=new RegExp(regStr); <BR>if(!re.test(card)){ <BR>alert("港澳居民身分證輸入不合法!正確格式如:A15263*(E)"); <BR>} <BR>} <BR>//殘障證明<BR>function canJicardRegExp(){ <BR>var card= document.getElementById("card"). value; <BR>var regStr="(^\d{22}$)|(^\d{20}$)"; <BR>var re=new RegExp(regStr); <BR>if(!re. test(card)){ <BR>alert("殘障證輸入不合法!"); <BR>} <BR>} <BR>//郵遞區號<BR>function postCardRegExp(){ <BR>var postCard= document.getElementById("postCard").value; <BR>var regStr="^[0-9]{6}$"; <BR>var re=new RegExp(regStr); <BR>if(!re. test(postCard)){ <BR>alert("輸入不合法!正確格式如4500!"); <BR>} <BR>} <BR>//傳真與固定電話校驗<BR>function telRegExp() { <BR>var tel= document.getElementById("tel").value; <BR>var regStr="((^\d{11}$)|(^\d{12}$))|(^\ d{3}-\d{8}$)|(^\d{4}-\d{7}$)|(^\d{4}-\d{8}$)"; <BR>var re=new RegExp(regStr); <BR>if(!re.test(tel)){ <BR>alert("書寫格式錯誤!正確格式如0511-44052** 或021-878888**"); <BR>} <BR>} <BR>//用戶名校驗,規則如下: <BR>//匹配帳號是否合法(字母開頭,允許5-16字節,允許字母數字下劃線<BR>function userNameRegExp( ){ <BR>var userName= document.getElementById("userName").value; <BR>var regStr="^[a-zA-Z][a-zA-Z0-9_]{3,15}$" ; <BR>var re=new RegExp(regStr); <BR>if(!re.test(userName)){ <BR>alert("必須以字母開頭,後面跟4-15位數字,字母或底線!"); <BR>} <BR>} <BR>//校驗整數<BR>function numRegExp(){ <BR>var num= document.getElementById("num").value; <BR>var document.getElementById("num").value; <BR>var document. "^[0-9]*[1-9][0-9]*$"; <BR>var re=new RegExp(regStr); <BR>if(!re.test(num)){ <BR>alert("填入格式錯誤,正確格式如:8080"); <BR>} <BR>} <BR>//校驗小數或整數<BR>function numFontRegExp(){ <BR>var num= document. getElementById("num").value; <BR>var regStr="^(([0]{1,})|([0-9]{1,})[.]{1}[0-9] {1,})|[0-9]{1,}$"; <BR>var re=new RegExp(regStr); <BR>if(!re.test(num)){ <BR>alert("填入格式錯誤,正確格式為12.3"); <BR>} <BR>} <BR>//驗證特殊字元^[^%$&@]{1,}$ <BR>function strRegExp(){ <BR>var str= document.getElementById("str").value; <BR>var regStr="^[^%$&@]{1,}$"; <BR>var re=new RegExp(regStr); <BR>if(!re.test(str)){ <BR>alert("不能包含特殊字元"); <BR>} <BR>} <BR>//驗證真實姓名<BR>function nameRegExp(){ <BR>var name= document.getElementById("name").value; <BR>var regStr="((^[a-zA-Z]{1}([a-zA-Z_]){2,20 })|(^[u4E00-u9FA5]{1} [a-zA-Z0-9u4E00-u9FA5]{1,14}))$"; <BR>var re=new RegExp(regStr); <BR>if (!re.test(name)){ <BR>alert("應為2-15個漢字或3-21個英文字母"); <BR>} <BR>} <BR>////驗證護照<BR>function huzhoaRegExp(){ <BR>var huzhao= document.getElementById("huzhao").value; <BR>var regStr="^((14)|(15)[0-9]{7}) |(G|S|D[0-9]{8})|((P.)|(S.)[0-9]{7})$"; <BR>var re=new RegExp(regStr) ; <BR>if(!re.test(huzhao)){ <BR>alert("護照輸入不合法,正確格式如:G00106556"); <BR>} <BR>} <BR>//軍官證<BR>function junGuanRegExp(){ <BR>var junguan= document.getElementById("junguan").value; <BR>var regStr="^[u4E00-u9FA5]{1}\d{7}$"; <BR>var re=new RegExp(regStr); <BR>if(!re.test(junguan)){ <BR>alert("輸入不合法!正確格式如:海6042**"); <BR>} <BR>} <BR>//文職幹部校驗<BR>function junGuanRegExp(){ <BR>var wenzhi= document.getElementById("wenzhi") .value; <BR>var regStr="^[u4E00-u9FA5]{1}文d{4,12}$"; <BR>var re=new RegExp(regStr); <BR>if(!re.test (wenzhi)){ <BR>alert("文職幹部證輸入不合法!正確格式如:廣文0816**"); <BR>} <BR>} <BR>//士兵證<BR>function shiBingRegExp (){ <BR>var shibing= document.getElementById("shibing").value; <BR>var regStr="^[u4E00-u9FA5]{1,3}\d{4,12}$"; <BR>var re=new RegExp(regStr); <BR>if(!re.test(shibing)){ <BR>alert("士兵證輸入不合法!正確格式如:鄂176340**"); <BR> } <BR>} <BR>//手機和固定電話校驗<BR>function iphoneTelRegExp(){ <BR>var iphoneTel= document.getElementById("iphoneTel").value; <BR>var regStr="(( (^\d{11}$)|(^\d{12}$))|(^\d{3}-\d{8}$)|(^\d{4}-\d{7} $)|(^\d{4}-\d{8}$))|(^((13[0-9])|(14[0-9])|(15[^4,\D] )|(18[0-9]))\d{8}$)$"; <BR>var re=new RegExp(regStr); <BR>if(!re.test(iphoneTel)){ <BR> alert("電話號碼書寫錯誤,不符合電話號碼規格(如0511-4405xxx 或15236565xxx),請重填"); <BR>} <BR>} <BR></script> </div>