Skip to content

Commit

Permalink
新增validatePassword验证密码方法
Browse files Browse the repository at this point in the history
  • Loading branch information
蔡金锋 committed Nov 13, 2020
1 parent deb939b commit 1aaa9f0
Show file tree
Hide file tree
Showing 7 changed files with 544 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ formatMoney('1000'); // => 1,000.00
- [isVehicle](https://doly-dev.github.io/util-helpers/module-Validator.html#.isVehicle) - 车牌号
- [isBankCard](https://doly-dev.github.io/util-helpers/module-Validator.html#.isBankCard) - 银行卡
- [isSocialCreditCode](https://doly-dev.github.io/util-helpers/module-Validator.html#.isSocialCreditCode) - 统一社会信用代码,也叫三证合一组织代码
- [isPassword](https://doly-dev.github.io/util-helpers/module-Validator.html#.isPassword) 密码强度
- <del>[isPassword](https://doly-dev.github.io/util-helpers/module-Validator.html#.isPassword) 密码强度</del>(即将废弃,请使用[validatePassword](https://doly-dev.github.io/util-helpers/module-Validator.html#.validatePassword)
- [isPassport](https://doly-dev.github.io/util-helpers/module-Validator.html#.isPassport) - 护照号
- [isChinese](https://doly-dev.github.io/util-helpers/module-Validator.html#.isChinese) - 中文
- [isIPv4](https://doly-dev.github.io/util-helpers/module-Validator.html#.isIPv4) - IPv4
- [isIPv6](https://doly-dev.github.io/util-helpers/module-Validator.html#.isIPv6) - IPv6
- [isUrl](https://doly-dev.github.io/util-helpers/module-Validator.html#.isUrl) - URL
- [isBusinessLicense](https://doly-dev.github.io/util-helpers/module-Validator.html#.isBusinessLicense) - 营业执照,也叫工商注册号
- [validatePassword](https://doly-dev.github.io/util-helpers/module-Validator.html#.validatePassword) - 验证密码
- 调试相关
- setDisableWarning - 禁止警告提示

Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export { default as isIPv4 } from './isIPv4';
export { default as isIPv6 } from './isIPv6';
export { default as isUrl } from './isUrl';
export { default as isBusinessLicense } from './isBusinessLicense';
export { default as validatePassword } from './validatePassword';

/**
* 数据处理
Expand Down
1 change: 1 addition & 0 deletions src/isPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function hasDisabled(val, chars = '') {
* @static
* @alias module:Validator.isPassword
* @since 1.1.0
* @deprecated 将在下次大版本更新后废弃,请使用 validatePassword
* @param {string} value 要检测的值
* @param {object} [options] 配置项
* @param {number} [options.level=2] 密码强度 1-包含一种字符 2-包含两种字符 3-包含三种字符。(大写字母、小写字母、数字、特殊字符)
Expand Down
12 changes: 9 additions & 3 deletions src/utils/config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const config = {
// 禁用警告
disableWarning: false
// 禁用warning提示
disableWarning: true
}

// 设置禁止警告提示
/**
* 设置禁止warning提示
* @static
* @alias module:Debug.formatBankCard
* @since 3.6.1
* @param {boolean} bool 是否禁止warning提示
*/
function setDisableWarning(bool) {
config.disableWarning(!!bool);
}
Expand Down
209 changes: 209 additions & 0 deletions src/validatePassword.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
import { config } from './utils/config';

const regNumber = /[\d]/;
const regLowerCaseLetter = /[a-z]/;
const regUpperCaseLetter = /[A-Z]/;
const regAllNumberAndLetter = /[\d|a-z]/ig;

// 是否包含数字
function hasNumber(val) {
return regNumber.test(val);
}

// 是否包含小写字母
function hasLowerCaseLetter(val) {
return regLowerCaseLetter.test(val);
}

// 是否包含大写字母
function hasUpperCaseLetter(val) {
return regUpperCaseLetter.test(val);
}

// 是否为十六进制
function hasHex(val) {
return val.indexOf('\\x') > -1 || val.indexOf('\\u') > -1;
}

// 是否包含特殊字符
function hasSpecialCharacter(val, chars = '') {
if (!chars) {
return false;
}

const specialChars = val.replace(regAllNumberAndLetter, '');
const regChars = hasHex(chars) ? new RegExp(`[${chars}]`) : null;

if (regChars) {
return regChars.test(specialChars);
}

let ret = false;
specialChars.split('').some((charItem) => {
if (chars.indexOf(charItem) > -1) {
ret = true;
}
return ret;
});
return ret;
}

// 是否包含非法字符
function hasUnallowableCharacter(val, chars = '') {
const specialChars = val.replace(regAllNumberAndLetter, '');

if (!chars && specialChars) {
return true;
}

const regChars = hasHex(chars) ? new RegExp(`[^${chars}]`) : null;
if (regChars) {
return regChars.test(specialChars);
}
let ret = false;
specialChars.split('').some((charItem) => {
if (chars.indexOf(charItem) === -1) {
ret = true;
}
return ret;
});
return ret;
}

/**
* 验证密码(数字、大小写字母、特殊字符、非法字符)
*
* @see {@link https://baike.baidu.com/item/ASCII#3|ASCII}
* @static
* @alias module:Validator.validatePassword
* @since 3.7.0
* @param {string} value 要检测的值
* @param {object} [options] 配置项
* @param {number} [options.level=2] 密码强度 1-包含一种字符 2-包含两种字符 3-包含三种字符。(大写字母、小写字母、数字、特殊字符)
* @param {boolean} [options.ignoreCase=false] 是否忽略大小写,为 ture 时,大小写字母视为一种字符
* @param {string} [options.special=!@#$%^&*()-=_+[]\|{},./?<>~] 支持的特殊字符
* @returns {object} 验证结果
* @example
*
* validatePassword('a12345678');
* // =>
* {
* validated: true, // 验证结果,根据密码强度、是否包含非法字符得出
* level: 2, // 强度级别
* containes: {
* number: true, // 包含数字
* lowerCaseLetter: true, // 包含小写字母
* upperCaseLetter: false, // 包含大写字母
* specialCharacter: false, // 包含特殊字符
* unallowableCharacter: false // 包含非法字符
* }
* }
*
* validatePassword('a12345678', {level: 3});
* // =>
* {
* validated: false,
* level: 2,
* containes: {
* number: true,
* lowerCaseLetter: true,
* upperCaseLetter: false,
* specialCharacter: false,
* unallowableCharacter: false
* }
* }
*
* validatePassword('_Aa一二三45678', {level: 3, ignoreCase: true});
* // =>
* {
* validated: false,
* level: 3,
* containes: {
* number: true,
* lowerCaseLetter: true,
* upperCaseLetter: true,
* specialCharacter: true,
* unallowableCharacter: true
* }
* }
*
* // 自定义特殊字符
* validatePassword('_Aa一二三45678', {level: 3, ignoreCase: true, special: '_一二三'});
* // =>
* {
* validated: true,
* level: 3,
* containes: {
* number: true,
* lowerCaseLetter: true,
* upperCaseLetter: true,
* specialCharacter: true,
* unallowableCharacter: false
* }
* }
*/
function validatePassword(value, {
level = 2,
ignoreCase = false,
special = "\\x21-\\x2F\\x3A-\\x40\\x5B-\\x60\\x7B-\\x7E"
} = {}) {
let valStr = value;

if (typeof value !== 'string') {
if (!config.disableWarning) {
console.warn(`[validatePassword] value must be a string.`);
}
valStr = '';
}

let currentLevel = 0;

// 包含数字
const containesNumber = hasNumber(valStr);
// 包含小写字母
const containesLowerCaseLetter = hasLowerCaseLetter(valStr);
// 包含大写字母
const containesUpperCaseLetter = hasUpperCaseLetter(valStr);
// 包含特殊字符
const containesSpecialCharacter = hasSpecialCharacter(valStr, special);
// 包含非法字符
const containesUnallowableCharacter = hasUnallowableCharacter(valStr, special);

if (containesNumber) {
currentLevel += 1;
}

if (ignoreCase) { // 不区分大小写
if (containesLowerCaseLetter || containesUpperCaseLetter) {
currentLevel += 1;
}
} else { // 区分大小写
if (containesLowerCaseLetter) {
currentLevel += 1;
}
if (containesUpperCaseLetter) {
currentLevel += 1;
}
}

if (containesSpecialCharacter) {
currentLevel += 1;
}

// 验证结果
const validated = currentLevel >= level && !containesUnallowableCharacter;

return {
validated,
level: currentLevel,
containes: {
number: containesNumber,
lowerCaseLetter: containesLowerCaseLetter,
upperCaseLetter: containesUpperCaseLetter,
specialCharacter: containesSpecialCharacter,
unallowableCharacter: containesUnallowableCharacter
}
}
}

export default validatePassword;
Loading

0 comments on commit 1aaa9f0

Please sign in to comment.