JavaScript:jQuery.validator与Form表单 - 验证多个输入框
检查身份证号码:
function checkCode(val) { var p = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/; 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]; var code = val.substring(17); if (p.test(val)) { var sum = 0; for (var i = 0; i < 17; i++) { sum += val[i] * factor[i]; } if (parity[sum % 11] == code.toUpperCase()) { return true; } } return false; }
手机号码:
/^1[34578]\d{9}$/.test(value)
用户名:
/^(?!_)(?!.*?_$)[a-zA-Z0-9\u4e00-\u9fa5]+$/.test(value)
姓名(中英文):
/^[a-zA-Z\u4e00-\u9fa5]{0,255}$/.test(value)
密码强度:
(function ($) { /* * 0-弱 * 1-中 * 2-强 */ var pswstrength = function () { } pswstrength.prototype = { constructor: pswstrength, //Unicode 编码区分数字,字母,特殊字符 CharMode: function (iN) { if (iN >= 48 && iN <= 57) //数字(U+0030 - U+0039) return 1; //二进制是0001 if (iN >= 65 && iN <= 90) //大写字母(U+0041 - U+005A) return 2; //二进制是0010 if (iN >= 97 && iN <= 122) //小写字母(U+0061 - U+007A) return 4; //二进制是0100 else //其他算特殊字符 return 8; //二进制是1000 }, bitTotal: function (num) { modes = 0; for (i = 0; i < 4; i++) { if (num & 1) //num不是0的话 modes++; //复杂度+1 num >>>= 1; //num右移1位 } return modes; }, check: function (sPW) { if (sPW.length < 7) //小于7位,直接“弱” return 0; Modes = 0; for (i = 0; i < sPW.length; i++) { //密码的每一位执行“位运算 OR” Modes |= this.CharMode(sPW.charCodeAt(i)); } return this.bitTotal(Modes); } } if (typeof $.pswstrength == 'undefined' || $.pswstrength == null) { $.pswstrength = new pswstrength(); } })(jQuery) $.pswstrength.check(value)
2