[前台]---js+jquery校验姓名,手机号,身份证号
生活随笔
收集整理的這篇文章主要介紹了
[前台]---js+jquery校验姓名,手机号,身份证号
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
記一次前臺(tái)驗(yàn)證姓名,手機(jī)號(hào),身份證號(hào)的代碼,復(fù)制即用,前提是有引入jquery.
var name = "姓名"if(!is_forbidName(name)){alert("姓名有誤!");}var phone = "18888888888";var validateTel = /^1[23456789]\d{9}/;if(!validateTel.test(phone)){alert("手機(jī)號(hào)有誤!");}var idCard = "000000000000000000";if(!regCardNo(idCard )){alert("身份證號(hào)有誤!");}//姓名校驗(yàn),正確返回true,不正確返回falsefunction is_forbidName(temp_str) {temp_str = temp_str.replace('*', "@");temp_str = temp_str.replace('--', "@");temp_str = temp_str.replace('/', "@");temp_str = temp_str.replace('+', "@");temp_str = temp_str.replace('\'', "@");temp_str = temp_str.replace('\\', "@");temp_str = temp_str.replace('$', "@");temp_str = temp_str.replace('^', "@");temp_str = temp_str.replace('.', "@");temp_str = temp_str.replace(';', "@");temp_str = temp_str.replace('<', "@");temp_str = temp_str.replace('>', "@");temp_str = temp_str.replace('"', "@");temp_str = temp_str.replace('=', "@");temp_str = temp_str.replace('{', "@");temp_str = temp_str.replace('}', "@");temp_str = temp_str.replace('#', "@");temp_str = temp_str.replace('。', "@");temp_str = temp_str.replace('!', "@");temp_str = temp_str.replace('!', "@");temp_str = temp_str.replace(' ', "@");var forbid_str = new String('@,%,~,&');var forbid_array = forbid_str.split(',');for (i = 0; i < forbid_array.length; i++) {if (temp_str.search(new RegExp(forbid_array[i])) != -1)return false;}return true;}//身份證校驗(yàn),正確返回true,不正確返回falsefunction regCardNo(idCardNum) {var regBox = {regIdCard : /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$///如果通過(guò)該驗(yàn)證,說(shuō)明身份證格式正確,但準(zhǔn)確性還需計(jì)算};// idCardNum = changeFivteenToEighteen(idCardNum);if(regBox.regIdCard.test(idCardNum)){if(idCardNum.length==18){var idCardWi=new Array( 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ); //將前17位加權(quán)因子保存在數(shù)組里var idCardY=new Array( 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ); //這是除以11后,可能產(chǎn)生的11位余數(shù)、驗(yàn)證碼,也保存成數(shù)組var idCardWiSum=0; //用來(lái)保存前17位各自乖以加權(quán)因子后的總和for(var i=0;i<17;i++){idCardWiSum+=idCardNum.substring(i,i+1)*idCardWi[i];}var idCardMod=idCardWiSum%11;//計(jì)算出校驗(yàn)碼所在數(shù)組的位置var idCardLast=idCardNum.substring(17);//得到最后一位身份證號(hào)碼//如果等于2,則說(shuō)明校驗(yàn)碼是10,身份證號(hào)碼最后一位應(yīng)該是Xif(idCardMod==2){if(idCardLast!="X" && idCardLast!="x"){return false;}}else{//用計(jì)算出的驗(yàn)證碼與最后一位身份證號(hào)碼匹配,如果一致,說(shuō)明通過(guò),否則是無(wú)效的身份證號(hào)碼if(idCardLast!=idCardY[idCardMod]){return false;}}}else{return false;}}else{return false;}return true;}總結(jié)
以上是生活随笔為你收集整理的[前台]---js+jquery校验姓名,手机号,身份证号的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 服务器任务栏不显示程序,Win10任务栏
- 下一篇: CoreAnimation编程指南(六)