jquery-validate验证身份证
生活随笔
收集整理的這篇文章主要介紹了
jquery-validate验证身份证
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
$(function() { jQuery.validator.addMethod("isIdCardNo", function (value, element){return this.optional(element) || isIdCardNo(value);},"請正確輸入您的身份證號碼");function isIdCardNo(num) {var factorArr = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5,8, 4, 2, 1];var parityBit = ["1", "0", "X", "9", "8", "7", "6", "5", "4","3", "2"];var varArray= [];var lngProduct = 0;var intCheckDigit;var intStrLen = num.length;var idNumber= num;//initializeif(intStrLen !== 18) {return false;}// check andset valuefor (i = 0;i < intStrLen; i++) {varArray[i] = idNumber.charAt(i);if ((varArray[i] < '0' || varArray[i]> '9') && (i !== 17)){return false;} else if (i < 17) {varArray[i] = varArray[i] * factorArr[i];}}if(intStrLen === 18) {//check datevar date8 = idNumber.substring(6, 14);if (!isDate8(date8)) {return false;}// calculate the sum of the productsfor (i = 0; i < 17; i++) {lngProduct = lngProduct + varArray[i];}// calculate the check digitintCheckDigit = parityBit[lngProduct % 11];// check last digitif (varArray[17] != intCheckDigit) {return false;}}else{return false;}return true;}function isDate8(sDate) {if(!/^[0-9]{8}$/.test(sDate)) {return false;}var year,month, day;year =sDate.substring(0, 4);month =sDate.substring(4, 6);day =sDate.substring(6, 8);var iaMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31];if (year< 1700 || year > 2500) {return false;}if (((year %4 == 0) && (year % 100 != 0)) ||(year % 400 == 0)) {iaMonthDays[1] = 29;}if (month< 1 || month > 12) {return false;}return !(day < 1 || day > iaMonthDays[month - 1]);}})
$("#form-member-add").validate({focusCleanup : true,rules:{card:{isIdCardNo:true,}}});
效果如下:
前些天發(fā)現(xiàn)了一個巨牛的人工智能學習網(wǎng)站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉(zhuǎn)到教程
博客若對你有用,歡迎掃碼打賞,你的打賞是我寫作的動力。
另外一個,沒試過,收藏了下:
function validateCard(code){// var code = $(this).val();var result = falsevar msg = ''var city = {11: '北京',12: '天津',13: '河北',14: '山西',15: '內(nèi)蒙古',21: '遼寧',22: '吉林',23: '黑龍江 ',31: '上海',32: '江蘇',33: '浙江',34: '安徽',35: '福建',36: '江西',37: '山東',41: '河南',42: '湖北 ',43: '湖南',44: '廣東',45: '廣西',46: '海南',50: '重慶',51: '四川',52: '貴州',53: '云南',54: '西藏 ',61: '陜西',62: '甘肅',63: '青海',64: '寧夏',65: '新疆',71: '臺灣',81: '香港',82: '澳門',91: '國外 '}if (!validatenull(code)) {if (code.length === 18) {if (!code || !/(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(code)) {msg = '證件號碼格式錯誤'} else if (!city[code.substr(0, 2)]) {msg = '地址編碼錯誤'} else {// 18位身份證需要驗證最后一位校驗位code = code.split('')// ∑(ai×Wi)(mod 11)// 加權因子var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]// 校驗位var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2, 'x']var sum = 0var ai = 0var wi = 0for (var i = 0; i < 17; i++) {ai = code[i]wi = factor[i]sum += ai * wi}if (parity[sum % 11] != code[17]) {msg = '證件號碼校驗位錯誤'} else {result = true}}} else {msg = '證件號碼長度不為18位'}} else {msg = '證件號碼不能為空'}if(!result)alert(msg)}function validatenull(val) {if (typeof val === 'boolean') {return false}if (typeof val === 'number') {return false}if (val instanceof Array) {if (val.length === 0) return true} else if (val instanceof Object) {if (JSON.stringify(val) === '{}') return true} else {if (val === 'null' || val == null || val === 'undefined' || val === undefined || val === '') return truereturn false}return false}總結
以上是生活随笔為你收集整理的jquery-validate验证身份证的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux模拟网络延迟,使用Nistne
- 下一篇: SPI通讯协议