插件窝 干货文章 如何用JavaScript高效校验密码复杂度,避免连续或重复字符?

如何用JavaScript高效校验密码复杂度,避免连续或重复字符?

password 字符 密码 options 903    来源:    2025-03-26

JavaScript密码复杂度校验方案

需求分析

要高效校验密码复杂度,通常需要满足以下要求: 1. 长度要求(如8-20位) 2. 包含多种字符类型(大小写字母、数字、特殊字符) 3. 避免连续字符(如"123"或"abc") 4. 避免重复字符(如"111"或"aaa")

实现方案

1. 基础复杂度校验

function validatePasswordBasic(password) {
    // 长度检查
    if (password.length < 8 || password.length > 20) {
        return false;
    }

    // 包含至少一个大写字母、小写字母、数字和特殊字符
    const hasUpper = /[A-Z]/.test(password);
    const hasLower = /[a-z]/.test(password);
    const hasNumber = /[0-9]/.test(password);
    const hasSpecial = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password);

    return hasUpper && hasLower && hasNumber && hasSpecial;
}

2. 防止连续字符校验

function hasSequentialChars(password, seqLength = 3) {
    for (let i = 0; i <= password.length - seqLength; i++) {
        // 检查数字序列(如123)
        const numSeq = password.substr(i, seqLength);
        if ('0123456789'.includes(numSeq) || '9876543210'.includes(numSeq)) {
            return true;
        }

        // 检查字母序列(如abc)
        const lower = password.toLowerCase().substr(i, seqLength);
        if ('abcdefghijklmnopqrstuvwxyz'.includes(lower)) {
            return true;
        }
    }
    return false;
}

3. 防止重复字符校验

function hasRepeatedChars(password, repeatCount = 3) {
    let count = 1;
    for (let i = 1; i < password.length; i++) {
        if (password[i] === password[i-1]) {
            count++;
            if (count >= repeatCount) {
                return true;
            }
        } else {
            count = 1;
        }
    }
    return false;
}

4. 综合校验函数

function validatePassword(password) {
    // 基础校验
    if (!validatePasswordBasic(password)) {
        return {
            valid: false,
            reason: "密码必须包含大小写字母、数字和特殊字符,长度8-20位"
        };
    }

    // 连续字符检查
    if (hasSequentialChars(password)) {
        return {
            valid: false,
            reason: "密码不能包含3个以上连续字符(如123或abc)"
        };
    }

    // 重复字符检查
    if (hasRepeatedChars(password)) {
        return {
            valid: false,
            reason: "密码不能包含3个以上重复字符(如111或aaa)"
        };
    }

    return {
        valid: true,
        reason: "密码强度符合要求"
    };
}

优化建议

  1. 性能优化:对于长密码,可以提前终止检查
  2. 可配置性:将规则参数化(如长度、字符类型要求等)
  3. 用户体验:提供实时强度反馈而非仅通过/不通过
  4. 安全性:避免在前端存储密码规则细节,后端应做最终验证

高级实现(支持配置)

class PasswordValidator {
    constructor(options = {}) {
        this.options = {
            minLength: 8,
            maxLength: 20,
            requireUpper: true,
            requireLower: true,
            requireNumber: true,
            requireSpecial: true,
            maxSequential: 3,
            maxRepeated: 3,
            ...options
        };
    }

    validate(password) {
        const errors = [];

        // 长度检查
        if (password.length < this.options.minLength) {
            errors.push(`密码至少需要${this.options.minLength}个字符`);
        }
        if (password.length > this.options.maxLength) {
            errors.push(`密码最多${this.options.maxLength}个字符`);
        }

        // 字符类型检查
        if (this.options.requireUpper && !/[A-Z]/.test(password)) {
            errors.push("密码必须包含至少一个大写字母");
        }
        if (this.options.requireLower && !/[a-z]/.test(password)) {
            errors.push("密码必须包含至少一个小写字母");
        }
        if (this.options.requireNumber && !/[0-9]/.test(password)) {
            errors.push("密码必须包含至少一个数字");
        }
        if (this.options.requireSpecial && !/[^A-Za-z0-9]/.test(password)) {
            errors.push("密码必须包含至少一个特殊字符");
        }

        // 连续字符检查
        if (this.hasSequentialChars(password)) {
            errors.push(`密码不能包含${this.options.maxSequential}个以上连续字符`);
        }

        // 重复字符检查
        if (this.hasRepeatedChars(password)) {
            errors.push(`密码不能包含${this.options.maxRepeated}个以上重复字符`);
        }

        return {
            valid: errors.length === 0,
            errors: errors.length > 0 ? errors : undefined
        };
    }

    hasSequentialChars(password) {
        for (let i = 0; i <= password.length - this.options.maxSequential; i++) {
            const seq = password.substr(i, this.options.maxSequential);
            if ('0123456789'.includes(seq) || '9876543210'.includes(seq)) {
                return true;
            }
            const lower = password.toLowerCase().substr(i, this.options.maxSequential);
            if ('abcdefghijklmnopqrstuvwxyz'.includes(lower)) {
                return true;
            }
        }
        return false;
    }

    hasRepeatedChars(password) {
        let count = 1;
        for (let i = 1; i < password.length; i++) {
            if (password[i] === password[i-1]) {
                count++;
                if (count >= this.options.maxRepeated) {
                    return true;
                }
            } else {
                count = 1;
            }
        }
        return false;
    }
}

// 使用示例
const validator = new PasswordValidator();
const result = validator.validate("Password123!");

这个实现提供了高度可配置的密码验证方案,可以根据具体需求调整各项参数。