<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-9\u4E00-\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 regStr="^[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-9\u4E00-\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> <br> </div>