fenzhi-utils
Version:
分值前端项目的js函数库
77 lines (73 loc) • 2.99 kB
JavaScript
/**
* 密码校验
* 规则:8-12位,包含大小写字母、数字、特殊字符
* @param {string} val
* @returns {obj} isTrue:是否通过 message:提示信息
*/
/*
CustomPasswordVerifiersFn('11111111');// {isTrue:false,message: '...'}
CustomPasswordVerifiersFn('Abcde');// {isTrue:false,message: '...'}
CustomPasswordVerifiersFn('Abcdefghigklmn');// {isTrue:false,message: '...'}
CustomPasswordVerifiersFn('Abcde123');// {isTrue:false,message: '...'}
CustomPasswordVerifiersFn('Abcdefg!');// {isTrue:false,message: '...'}
CustomPasswordVerifiersFn('abcdefg1!');// {isTrue:false,message: '...'}
CustomPasswordVerifiersFn('Abcde12!');// {isTrue:true,message: '验证通过'}
*/
export function CustomPasswordVerifiersFn(val) {
let valArr = val.split('');
let illegalStr = []; // 非法字符有哪些
let verification1 = `ABCDEFGHIJKLMNOPQRSTUVWXYZ`;
let verification2 = `abcdefghijklmnopqrstuvwxyz`;
let verification3 = `0123456789`;
let verification4 = `!@#$&*.`;
let verificatType1 = []; // 大写字母收集
let verificatType2 = []; // 小写字母收集
let verificatType3 = []; // 数字类型收集
let verificatType4 = []; // 特殊字母收集
let verificationStr = verification1
.concat(verification2)
.concat(verification3)
.concat(verification4);
valArr.forEach((item) => {
if (verificationStr.includes(item)) {
if (verification1.includes(item)) {
verificatType1.push(item);
} else if (verification2.includes(item)) {
verificatType2.push(item);
} else if (verification3.includes(item)) {
verificatType3.push(item);
} else if (verification4.includes(item)) {
verificatType4.push(item);
}
} else {
illegalStr.push(item);
}
});
let signArr = [];
valArr.length < 8 && signArr.push('密码长度小于8位');
valArr.length > 12 && signArr.push('密码长度大于12位');
!verificatType1.length && signArr.push('未包含大写字母');
!verificatType2.length && signArr.push('未包含小写字母');
!verificatType3.length && signArr.push('未包含数字');
!verificatType4.length && signArr.push('未包含特殊符号');
illegalStr.length && signArr.push(`包含未指定字符:${signArr.join('')}`);
if (signArr.length) {
signArr.unshift('密码校验存在以下问题:');
signArr.push('');
signArr.push('温馨提示:');
signArr.push('密码规则:8-12位,包含大小写字母、数字、特殊符号');
signArr.push(`大写字母取值范围:${verification1}`);
signArr.push(`小写字母取值范围:${verification2}`);
signArr.push(`数字取值范围:${verification3}`);
signArr.push(`特殊符号取值范围:${verification4}`);
return {
isTrue: false,
message: signArr.join('\n'),
};
} else {
return {
isTrue: true,
message: '验证通过',
};
}
}