Skip to content

Commit

Permalink
优化密码强验证
Browse files Browse the repository at this point in the history
  • Loading branch information
luoanb committed Mar 12, 2024
1 parent 182a2a6 commit 9f37925
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions src/Verifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@ export type IVerificationItem<V> = Partial<{
msg: string
}>

/**
* 错误验证参数
*/
export type PasswordVerifProps = Partial<{
/** 最小字符数 */
min: number
/** 最大字符数 */
max: number
/** 错误提醒 */
msg: string
/**
* 至少一位字母:默认:true
* @default true
*/
mustLatter: boolean
/**
* 至少一位数字:默认:true
* @default true
*/
mustNumber: boolean
/**
* 至少一位特殊字符:默认:true
* @default true
*/
mustSpecial: boolean
}>

/**
* 基础校验规则
*/
Expand Down Expand Up @@ -154,7 +181,14 @@ export class Verifications {
* @param props.max =16
* @param props.msg = 请输入字母、数字、特殊字符,${min}-${max}位
*/
static password({ min = 6, max = 16, msg = '' } = {}) {
static password({
min = 6,
max = 16,
msg = '',
mustLatter = true,
mustNumber = true,
mustSpecial = true
}: PasswordVerifProps = {}) {
const defaultMsg = `请输入字母、数字、特殊字符(至少各一位),${min}-${max}位`
return {
async execute(password) {
Expand All @@ -172,16 +206,22 @@ export class Verifications {
return false
}
// 检查是否包含至少一个字母
if (!hasLetter.test(password)) {
return false
if (mustLatter) {
if (!hasLetter.test(password)) {
return false
}
}
// 检查是否包含至少一个数字
if (!hasNumber.test(password)) {
return false
if (mustNumber) {
if (!hasNumber.test(password)) {
return false
}
}
// 检查是否包含至少一个特殊字符
if (!hasSpecialChar.test(password)) {
return false
if (mustSpecial) {
if (!hasSpecialChar.test(password)) {
return false
}
}
// 如果所有条件都满足,则密码有效
return true
Expand Down

0 comments on commit 9f37925

Please sign in to comment.