validata.htm 复制代码代码如下: ; ;无标题文档<脚本 src="lib/jquery.validate.js" type="text/javascript"><脚本src="lib/card.js" type="text/javascript"> script><br><style type="text/css"><br> em.success{<br> 背景:url("images/tips_arrow.gif") 无重复左 0px;<br> 填充- left:16px;<br> margin-left:2px;<br> }<br> em.error{<br> 背景:url("images/tips_arrow.gif") 不重复左-51px;<br> 显示:inline;<br> padding-left:10px;<br> font-style:normal;<br> 字体大小:11px;<br> margin-left:2px;<br> font-family:12px/162% Arial、Helvetica、无衬线体;<br><br> }<br></style> <p></头></p> <p></p> <br> <form class="cmsform" id="commentForm" method="get" action=""> <br> <p><br> <label for=" cusername"> <input id="cusername" name="username" size="25"><br><br> </p><br> <p><br> <label for="cemail"> <br> </p><br> <p><br> <label for="card"> <input id="card" name="card" size="25"/><br><br> </p> <br> <p><br> <label for="passport">护照编号</label><em>*</em><br> <input id="passport" name="passport" size="25"><br><br> </p><br> <p><br> <label for="phone"> <input id="phone" name="phone" size="25"><br> </label></label></p>><br> form><br></body><br></html><br></label></label></label></p> </form> </div> <br>validata.js<br><div class="codetitle"> <span><a style="CURSOR: pointer" data="15976" class="copybut" id="copybut15976" onclick="doCopy('code15976')"><u>复制代码</u> </a></span>代码如下:</div> <div class="codebody" id="code15976"> <br> $(function(){<br><br> $.validator.setDefaults({ <br> submitHandler: function(form) { <br> form.submit(); <br> } <br> }); <br> // 字符验证 <br> jQuery.validator.addMethod("stringCheck", function(value, element) { <br> return this.optional(element) || /^[u0391-uFFE5w] $/.test(value); <br> }, "只能包括中文字、英文字母、数字和下划线"); <br> // 中文字两个字节 <br> jQuery.validator.addMethod("byteRangeLength", function(value, element, param) { <br> var length = value.length; <br> for(var i = 0; i < value.length; i ){ <BR> if(value.charCodeAt(i) > 127){ <br> length ; <br> } <br> } <br> return this.optional(element) || ( length >= param[0] && length <= param[1] ); <BR>}, "请确保输入的值在3-15个字节之间(一个中文字算2个字节)"); <br><br>// 身份证号码验证 <BR>jQuery.validator.addMethod("isIdCardNo", function(value, element) { <BR> return this.optional(element) || idCardNoUtil.checkIdCardNo(value); <BR>}, "请正确输入您的身份证号码"); <BR>//护照编号验证<BR> jQuery.validator.addMethod("passport", function(value, element) { <BR> return this.optional(element) || checknumber(value); <BR>}, "请正确输入您的护照编号"); <br><br>// 手机号码验证 <BR>jQuery.validator.addMethod("isMobile", function(value, element) { <BR> var length = value.length; <BR> var mobile = /^(((13[0-9]{1})|(15[0-9]{1})) d{8})$/; <BR> return this.optional(element) || (length == 11 && mobile.test(value)); <BR>}, "请正确填写您的手机号码"); <br><br>// 电话号码验证 <BR>jQuery.validator.addMethod("isTel", function(value, element) { <BR> var tel = /^d{3,4}-?d{7,9}$/; //电话号码格式010-12345678 <BR> return this.optional(element) || (tel.test(value)); <BR>}, "请正确填写您的电话号码"); <br><br>// 联系电话(手机/电话皆可)验证 <BR>jQuery.validator.addMethod("isPhone", function(value,element) { <BR> var length = value.length; <BR> var mobile = /^(((13[0-9]{1})|(15[0-9]{1})) d{8})$/; <BR> var tel = /^d{3,4}-?d{7,9}$/; <BR> return this.optional(element) || (tel.test(value) || mobile.test(value)); <br><br>}, "请正确填写您的联系电话"); <br><br>// 邮政编码验证 <BR>jQuery.validator.addMethod("isZipCode", function(value, element) { <BR> var tel = /^[0-9]{6}$/; <BR> return this.optional(element) || (tel.test(value)); <BR>}, "请正确填写您的邮政编码"); <br><br>//开始验证 <BR>$('#commentForm').validate({ <br><br> rules: { <BR> username: { <BR> required:true, <BR> stringCheck:true, <BR> byteRangeLength:[3,15] <BR> }, <BR> email:{ <BR> required:true, <BR> email:true <BR> }, <BR> phone:{ <BR> required:true, <BR> isMobile:true <BR> }, <BR> address:{ <BR> required:true, <BR> stringCheck:true, <BR> byteRangeLength:[3,100] <BR> },<BR> card:{<BR> required:true,<BR> isIdCardNo:true<br><br> },<BR> passport:{<BR> required:true,<BR> passport:true<br><br> }<BR> }, <br><br> <BR> messages:{ <BR> username: { <BR> required: "请填写用户名", <BR> stringCheck: "用户名只能包括中文字、英文字母、数字和下划线", <BR> byteRangeLength: "用户名必须在3-15个字符之间(一个中文字算2个字符)" <BR> }, <BR> email:{ <BR> required: "<font color=red>请输入一个Email地址</fond>", <br> email: "请输入一个有效的Email地址" <br> }, <br> phone:{ <br> required: "请输入您的联系电话", <br> isPhone: "请输入一个有效的联系电话" <br> }, <br> address:{ <br> required: "请输入您的联系地址", <br> stringCheck: "请正确输入您的联系地址", <br> byteRangeLength: "请详实您的联系地址以便于我们联系您" <br> },<br> card:{<br> required:"请输入身份证号",<br> isIdCardNo:"请输入正确的身份证号"<br> },<br> passport:{<br> required:"请输入护照编号",<br> passport:"请输入正确的护照编号"<br> }<br> }, <br><br> <br> focusInvalid: false, <br> onkeyup: false, <br><br> <br> errorPlacement: function(error, element) { <br> error.appendTo( element.parent()); <br> }, <br> errorElement:"em",<br> error:function(label){label.text(" ").addClass("error");}<br> }); <br>})<br> </div> <br>card.js<br><div class="codetitle"> <span><a style="CURSOR: pointer" data="93950" class="copybut" id="copybut93950" onclick="doCopy('code93950')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code93950"> <br>var idCardNoUtil = { <p>provinceAndCitys: {11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",<br>31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",<br>45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",<br>65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"},</p> <p><br>powers: ["7","9","10","5","8","4","2","1","6","3","7","9","10","5","8","4","2"],</p> <p><br>parityBit: ["1","0","X","9","8","7","6","5","4","3","2"],</p> <p><br>genders: {male:"男",female:"女"},</p> <p><br>checkAddressCode: function(addressCode){<br>var check = /^[1-9]\d{5}$/.test(addressCode);<br>if(!check) return false;<br>if(idCardNoUtil.provinceAndCitys[parseInt(addressCode.substring(0,2))]){<br>return true;<br>}else{<br>return false;<br>}<br>},</p> <p><br>checkBirthDayCode: function(birDayCode){<br>var check = /^[1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))$/.test(birDayCode);<br>if(!check) return false;<br>var yyyy = parseInt(birDayCode.substring(0,4),10);<br>var mm = parseInt(birDayCode.substring(4,6),10);<br>var dd = parseInt(birDayCode.substring(6),10);<br>var xdata = new Date(yyyy,mm-1,dd);<br>if(xdata > new Date()){<br>return false;//生日不能大于当前日期<br>}else if ( ( xdata.getFullYear() == yyyy ) && ( xdata.getMonth () == mm - 1 ) && ( xdata.getDate() == dd ) ){<br>return true;<br>}else{<br>return false;<br>}<br>},</p> <p><br>getParityBit: function(idCardNo){<br>var id17 = idCardNo.substring(0,17);</p> <p>var power = 0;<br>for(var i=0;i<17;i++){<br>power += parseInt(id17.charAt(i),10) * parseInt(idCardNoUtil.powers[i]);<br>}</p> <p>var mod = power % 11;<br>return idCardNoUtil.parityBit[mod];<br>},</p> <p><br>checkParityBit: function(idCardNo){<br>var parityBit = idCardNo.charAt(17).toUpperCase();<br>if(idCardNoUtil.getParityBit(idCardNo) == parityBit){<br>return true;<br>}else{<br>return false;<br>}<br>},</p> <p><br>checkIdCardNo: function(idCardNo){<br>//15位和18位身份证号码的基本校验<br>var check = /^\d{15}|(\d{17}(\d|x|X))$/.test(idCardNo);<br>if(!check) return false;<br>//判断长度为15位或18位<br>if(idCardNo.length==15){<br>return idCardNoUtil.check15IdCardNo(idCardNo);<br>}else if(idCardNo.length==18){<br>return idCardNoUtil.check18IdCardNo(idCardNo);<br>}else{<br>return false;<br>}<br>},</p> <p>//校验15位的身份证号码<br>check15IdCardNo: function(idCardNo){<br>//15位身份证号码的基本校验<br>var check = /^[1-9]\d{7}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))\d{3}$/.test(idCardNo);<br>if(!check) return false;<br>//校验地址码<br>var addressCode = idCardNo.substring(0,6);<br>check = idCardNoUtil.checkAddressCode(addressCode);<br>if(!check) return false;<br>var birDayCode = '19' + idCardNo.substring(6,12);<br>//校验日期码<br>return idCardNoUtil.checkBirthDayCode(birDayCode);<br>},</p> <p>//校验18位的身份证号码<br>check18IdCardNo: function(idCardNo){<br>//18位身份证号码的基本格式校验<br>var check = /^[1-9]\d{5}[1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))\d{3}(\d|x|X)$/.test(idCardNo);<br>if(!check) return false;<br>//校验地址码<br>var addressCode = idCardNo.substring(0,6);<br>check = idCardNoUtil.checkAddressCode(addressCode);<br>if(!check) return false;<br>//校验日期码<br>var birDayCode = idCardNo.substring(6,14);<br>check = idCardNoUtil.checkBirthDayCode(birDayCode);<br>if(!check) return false;<br>//验证校检码<br>return idCardNoUtil.checkParityBit(idCardNo);<br>},</p> <p>formateDateCN: function(day){<br>var yyyy =day.substring(0,4);<br>var mm = day.substring(4,6);<br>var dd = day.substring(6);<br>return yyyy + '-' + mm +'-' + dd;<br>},</p> <p>//获取信息<br>getIdCardInfo: function(idCardNo){<br>var idCardInfo = {<br>gender:"", //性别<br>birthday:"" // 出生日期(yyyy-mm-dd)<br>};<br>if(idCardNo.length==15){<br>var aday = '19' + idCardNo.substring(6,12);<br>idCardInfo.birthday=idCardNoUtil.formateDateCN(aday);<br>if(parseInt(idCardNo.charAt(14))%2==0){<br>idCardInfo.gender=idCardNoUtil.genders.female;<br>}else{<br>idCardInfo.gender=idCardNoUtil.genders.male;<br>}<br>}else if(idCardNo.length==18){<br>var aday = idCardNo.substring(6,14);<br>idCardInfo.birthday=idCardNoUtil.formateDateCN(aday);<br>if(parseInt(idCardNo.charAt(16))%2==0){<br>idCardInfo.gender=idCardNoUtil.genders.female;<br>}else{<br>idCardInfo.gender=idCardNoUtil.genders.male;<br>}</p> <p>}<br>return idCardInfo;<br>},</p> <p><br>getId15:function(idCardNo){<br>if(idCardNo.length==15){<br>return idCardNo;<br>}else if(idCardNo.length==18){<br>return idCardNo.substring(0,6) + idCardNo.substring(8,17);<br>}else{<br>return null;<br>}<br>},</p> <p><br>getId18: function(idCardNo){<br>if(idCardNo.length==15){<br>var id17 = idCardNo.substring(0,6) '19' idCardNo.substring(6); <br>var parityBit = idCardNoUtil.getParityBit(id17);<br>return id17 parityBit;<br>}else if(idCardNo.length==18){<br>return idCardNo;<br>}else{<br> return null;<br>}<br>}<br>};<br>//パスポートが正しいかどうかを確認します<br>function checknumber(number){<br>var str=number;<br>//In JavaScript の場合、正規表現は「/」でのみ開始および終了でき、二重引用符は使用できません。 <br>var Expression=/(Pd{7})|(Gd{8})/;<br>var objExp=new RegExp(Expression );<br>if(objExp.test(str)==true){<br> return true;<br>}else{<br> return false;<br>} <br>};<br></p> </div>