JavaScript:jQuery.validator与Form表单 - 验证密码合法性
验证密码合法性:
<script> (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) </script> <script> function checkForm() { var myForm = {}; $.each($('form').serializeArray(), function (i, field) { myForm[field.name] = field.value; }); if (!myForm.oldPwd.length) { alert("旧密码不能为空!") return false; } if (!myForm.newPwd.length) { alert("新密码不能为空!") return false; } if (!myForm.comfirm.length) { alert("确认密码不能为空!") return false; } if (myForm.newPwd.length < 8 || myForm.newPwd.length > 20) { alert("密码长度必须是8-20!") return false; } if (myForm.newPwd != myForm.comfirm) { alert("两次输入的密码不一致!") return false; } if ($.pswstrength.check(myForm.newPwd) < 3) { alert("新密码强度过低!"); return false; } return true; } function submitForm() { if (checkForm()) { $.ajax({ method: "POST", url: "/User/ResetPasswd", data: $('form').serialize(), success: function (res) { if (res.errno == 0) { alert(res.message); location.href = "/"; } else { alert("错误码:" + res.errno + ", " + res.message); } } }); } } </script>
jquery.validate.js
<script> (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) </script> <script> jQuery.validator.addMethod("stongerPassword", function (value) { if ($.pswstrength.check(value) < 3) { return false; } return true; }, "新密码强度过低,至少应包含数字,小写字母,大写字母,特殊符号其中三种!"); $().ready(function () { $("form").validate({ focusInvalid: true, submitHandler: function () { $.ajax({ method: "POST", url: "/User/ResetPasswd", data: $('form').serialize(), success: function (res) { if (res.errno == 0) { alert(res.message); location.href = "/"; } else { alert("错误码:" + res.errno + ", " + res.message); } } }); }, rules: { signName: "required", oldPwd: { required: true, rangelength: [6, 20], }, newPwd: { required: true, rangelength: [8, 20], stongerPassword: true, }, comfirm: { required: true, rangelength: [8, 20], equalTo: "#newPwd" } }, messages: { signName: "获取账户失败,请刷新后重试!", oldPwd: { required: "旧密码不能为空!", rangelength: "密码长度必须为:{0}-{1}之间", }, newPwd: { required: "新密码不能为空!", rangelength: "密码长度必须为:{0}-{1}之间", }, comfirm: { required: "确认密码不能为空!", rangelength: "密码长度必须为:{0}-{1}之间", equalTo: "两次输入的密码不一致!", } } }); }) </script> <style> label.error { color: red; } </style>
2