/**
* 密码校验 v1.1.0
*
参数示例
1.{}
2.{ minLength: 6, // 最小长度 middleLength: 12, // 中间长度
maxLength: 18, //最长长度 strongTextArr: ['弱', '中', '强'] // 密码强度的描述 }
调用示例
var a1 = validPassword().init({});
var validLevel = a1.validPwdStrong('231312313123_');
console.log(validLevel ? a1.validMsg:validLevel);
*/
var validPassword = function () {
var validPasswordObj = {
validMsg:'',
minLength:0,
middleLength:0,
maxLength:0,
strongTextArr:[],
init: function (options) {
if (options) {
this.minLength = options.minLength || 6;
this.middleLength = options.middleLength || 12;
this.maxLength = options.maxLength || 18;
this.strongTextArr = options.strongTextArr || ['弱', '中', '强'];
return this;
} else {
console.error('eg: validPassword().init({}), at last input "{}" ');
return undefined;
}
},
isNormal:function (value) {
if (value == '') {
this.validMsg = '密码不能为空';
}
if (~value.indexOf(" ")) {
this.validMsg = '密码存在空格';
}
if (/(^\_)|(\__)|(\_+$)/.test(value)) {
this.validMsg = '密码首尾不能出现下划线\'_\'';
}
},
validPwdStrong:function (pwd) {
pwd = pwd.trim();
this.isNormal(pwd);
if (this.validMsg == '') {
var reg11 = /\d/;
var reg22 = /[a-zA-Z]/;
var reg33 = /[!@#$%^&*`~()-+=]/;
if (reg11.test(pwd) && reg22.test(pwd) && reg33.test(pwd)) {
if (pwd.length < this.minLength) {
//密码等级增加
return this.strongTextArr[0];
} else if (pwd.length >= this.minLength && pwd.length < this.middleLength) {
return this.strongTextArr[1];
} else if (pwd.length >= this.middleLength && pwd.length < this.maxLength) {
return this.strongTextArr[2];
}
} else {
this.validMsg = '必须为大小写字母、数字和特殊字符的组合,长度为'
+ this.minLength + '至' + this.maxLength + '位';
return '0';
}
}else{
return '0';
}
}
}
return validPasswordObj;
}
js校验密码复杂度(必须包含数字、字母、英文和特殊符号)
于 2020-03-09 16:18:04 首次发布