Javascript regular expression password validation having special characters

后端 未结 7 710
闹比i
闹比i 2020-12-12 17:14

I am trying to validate the password using regular expression. The password is getting updated if we have all the characters as alphabets. Where am i going wrong ? is the re

相关标签:
7条回答
  • 2020-12-12 18:04

    Don't try and do too much in one step. Keep each rule separate.

    function validatePassword() {
        var p = document.getElementById('newPassword').value,
            errors = [];
        if (p.length < 8) {
            errors.push("Your password must be at least 8 characters");
        }
        if (p.search(/[a-z]/i) < 0) {
            errors.push("Your password must contain at least one letter."); 
        }
        if (p.search(/[0-9]/) < 0) {
            errors.push("Your password must contain at least one digit.");
        }
        if (errors.length > 0) {
            alert(errors.join("\n"));
            return false;
        }
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题