简体   繁体   English

正则表达式密码验证不起作用 - javascript

[英]Regex password validation not working - javascript

I have the javascript below.我有下面的 javascript。 I am trying to put in place password validation rules to have the following:我正在尝试制定密码验证规则以具有以下内容:

  • At least 12 characters long (works)至少 12 个字符长(有效)
  • At least 1 letter (works)至少 1 封信(有效)
  • At least 1 uppercase letter (doesn't work)至少 1 个大写字母(无效)
  • At least 1 digit (works)至少 1 位数(有效)
  • At least 1 special character (doesn't work)至少 1 个特殊字符(不起作用)

Could I please get pointers on how to get the uppercase & special characters validations to work?我能否获得有关如何使大写和特殊字符验证起作用的指示?


// Register Handle
router.post('/register', (req, res) => {
    const { name, email, password, password2, userType } = req.body;

    let errors = [];

    // Check required fields
    if(!name || !email || !password || !password2 || !userType){
        errors.push({ msg: 'Please fill in all fields'})
    }

    // Check if passwords match
    if(password !== password2) {
        errors.push({ msg: 'Passwords do not match' });
    }

    // Check password length
    if(password.length < 12) {
        errors.push({ msg: 'Password should be at least 12 characters long' });
    }

    // Check if password contains at least 1 letter
    if(password.search(/[a-z]/i) < 0){
        errors.push({ msg: 'Password should contain at least one letter' });
    }

    // Check if password contains at least 1 Uppercase letter
    if(password.search(/[A-Z]/i) < 0){
        errors.push({ msg: 'Password should contain at least one uppercase character' });
    }

    // Check if password contains at least 1 number
    if(password.search(/[0-9]/) < 0){
        errors.push({ msg: 'Password should contain at least one number' });
    }

    // Check if password contains at least 1 special character
    if(password.search() < 0){
        errors.push({ msg: 'Password should contain at least one number' });
    }

    if(errors.length > 0) {
        res.render('register', {
            errors,
            name,
            email,
            password,
            password2,
            userType
        })
    } else {
        // Passed Validation
        User.findOne({ email: email })
            .then(user => {
                if(user) {
                    // User exists
                    errors.push({ msg: 'Email already registered' });
                    res.render('register', {
                        errors,
                        name,
                        email,
                        password,
                        password2,
                        userType
                    })
                } else {
                    const newUser = new User({
                        name,
                        email,
                        password,
                        userType
                    });

                    // Hash Password
                    bcrypt.genSalt(10, (err, salt) => bcrypt.hash(newUser.password, salt, (err, hash) =>{
                        if(err) throw err;
                        // Sets password to hash
                        newUser.password = hash;

                        // Save user
                        newUser.save()
                            .then(user => {
                                req.flash('success_msg', 'Registration successful! Login to continue')
                                res.redirect('/auth/login')
                                return;
                            })
                            .catch(err => console.log(err));
                    }))
                }
            })
    }
});


Letter and digit checks should be updated as follows:字母和数字检查应更新如下:

    // Check if password contains at least 1 letter
    if(/[a-z]/i.test(password)){
        errors.push({ msg: 'Password should contain at least one letter' });
    }

    // Check if password contains at least 1 Uppercase letter
    if(/[A-Z]/.test(password)){
        errors.push({ msg: 'Password should contain at least one uppercase character' });
    }

    // Check if password contains at least 1 number
    if(/[0-9]/.test(password)){
        errors.push({ msg: 'Password should contain at least one number' });
    }

Better use test instead of search when checking if an expression can match anything in the string, and /[AZ]/i matches any letters case-insensitively.在检查表达式是否可以匹配字符串中的任何内容时,最好使用test而不是search ,并且/[AZ]/i不区分大小写匹配任何字母。

PS If you want to check special characters see Check for special characters in string . PS 如果要检查特殊字符,请参阅检查字符串中的特殊字符 There are heaps of ways to match special characters in JavaScript.有很多方法可以匹配 JavaScript 中的特殊字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM