Javascript regular expression password validation having special characters

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 regular expression right ?

function validatePassword() {
    var newPassword = document.getElementById('changePasswordForm').newPassword.value;
    var minNumberofChars = 6;
    var maxNumberofChars = 16;
    var regularExpression  = /^[a-zA-Z0-9!@#$%^&*]{6,16}$/;
    alert(newPassword); 
    if(newPassword.length < minNumberofChars || newPassword.length > maxNumberofChars){
        return false;
    }
    if(!regularExpression.test(newPassword)) {
        alert("password should contain atleast one number and one special character");
        return false;
    }
}

91 thoughts on “Javascript regular expression password validation having special characters”

  1. 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;
    }
    
    Reply
  2. If you check the length seperately, you can do the following:

    var regularExpression  = /^[a-zA-Z]$/;
    
    if (regularExpression.test(newPassword)) {
        alert("password should contain atleast one number and one special character");
        return false;
    } 
    
    Reply
  3. Use positive lookahead assertions:

    var regularExpression = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,16}$/;
    

    Without it, your current regex only matches that you have 6 to 16 valid characters, it doesn’t validate that it has at least a number, and at least a special character. That’s what the lookahead above is for.

    • (?=.*[0-9]) – Assert a string has at least one number;
    • (?=.*[!@#$%^&*]) – Assert a string has at least one special character.
    Reply
  4. After a lot of research, I was able to come up with this. This has more special characters

    validatePassword(password) {
            const re = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()+=-\[\]\\';,./{}|\":<>? ~_]).{8,}/
            return re.test(password);
        }
    
    Reply
  5. Regex for password:

    /^(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[a-zA-Z!#$%&? "])[a-zA-Z0-9!#$%&?]{8,20}$/

    Took me a while to figure out the restrictions, but I did it!

    Restrictions: (Note: I have used >> and << to show the important characters)

    1. Minimum 8 characters {>>8,20}
    2. Maximum 20 characters {8,>>20}
    3. At least one uppercase character (?=.*[A-Z])
    4. At least one lowercase character (?=.*[a-z])
    5. At least one digit (?=.*\d)
    6. At least one special character (?=.*[a-zA-Z >>!#$%&? "<<])[a-zA-Z0-9 >>!#$%&?<< ]
    Reply
  6. you can make your own regular expression for javascript validation

        /^            : Start
        (?=.{8,})        : Length
        (?=.*[a-zA-Z])   : Letters
        (?=.*\d)         : Digits
        (?=.*[!#$%&? "]) : Special characters
        $/              : End
    
    
    
            (/^
            (?=.*\d)                //should contain at least one digit
            (?=.*[a-z])             //should contain at least one lower case
            (?=.*[A-Z])             //should contain at least one upper case
            [a-zA-Z0-9]{8,}         //should contain at least 8 from the mentioned characters
    
            $/)
    
    Example:-   /^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{7,}$/
    
    Reply
  7. I use the following script for min 8 letter password, with at least a symbol, upper and lower case letters and a number

    function checkPassword(str)
    {
        var re = /^(?=.*\d)(?=.*[!@#$%^&*])(?=.*[a-z])(?=.*[A-Z]).{8,}$/;
        return re.test(str);
    }
    
    Reply
  8. 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;
    }
    

    There is a certain issue in below answer as it is not checking whole string due to absence of [ ] while checking the characters and numerals, this is correct version

    Reply

Leave a Comment