1

I'm trying to develop a regex expression for a password validation program that I am making with JavaScript.

Requirements

The password must be at least seven characters in length.

Must contain at least one

  • upper case letter (A-Z)
  • lower case letter (a-z)
  • number (0-9)
  • special symbol (!?#@)

I'm struggling to find the right regex expression to check for all of those components. The password doesn't have to be in any specific order. I thought regex would be the easiest method for password verification, but now I'm not so sure.

Thanks in advance.

2
  • 1
    What things you have tried? Commented May 11, 2015 at 3:51
  • 1
    I thing having multiple test rather than having a messing single regex will be easier Commented May 11, 2015 at 3:51

5 Answers 5

2

I think having separate test for each case will be easier.

var valid = p.length > 6 && // At least 7 characters
/[a-z]/.test(p) && // At least one lowercase letter (a-z)
/[A-Z]/.test(p) && // At least one uppercase letter (A-Z)
/\d/.test(p) && // At least one number (0-9)
/[@#$%]/.test(p); // At least one special symbol
Sign up to request clarification or add additional context in comments.

1 Comment

Try /[^a-zA-Z0-9]/ for the special character check
1

Try this I am sharing a JavaScript code:

function CheckPassword(inputtxt)   
{   
    var paswd = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{7,15}$/;  
    if (inputtxt.value.match(paswd))   
    {   
        alert('Correct, try another...')  
        return true;  
    }  
    else  
    {   
        alert('Wrong...!')  
        return false;  
    }  
}    

Comments

0

Try this am not sure this satisfy your need completely or not!!

/(?=.*\d)(?=.*[!@#\$%\^\&*\)\(+=._-])(?=.*[a-z])(?=.*[A-Z]).{7,}/;

 (?=.*\d) : Check for digit

 `(?=.*[a-z])` : Check for atleast one lower case

 `(?=.*[A-Z])` : Check for atleast one upper case

{7,}        : Check for min of 7 character

(?=.*[!@#\$%\^\&*\)\(+=._-]) : Check for special characters

Hope this helps!

Comments

0
/(?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d)(?=.*?[^A-Za-z0-9\s]).{7}/.test(password)

Positive lookaheads are used to test for the "at least one of" conditions. Finally, we just make sure that 7 characters except newline can match, and we're done.

For "special symbols", we're using the negated character class [^A-Za-z0-9\s], but this is obviously flexible and you can use something else that fits your needs.

Comments

0

You can try something like this. Separate Regex for different validation rules.

checkPwd = function () {
    var str = document.getElementById('password').value;
    if (str.length < 7) {
        alert("too_short");
        return ("too_short");
    } else if (str.search(/\d/) == -1) {
        alert("no_num");
        return ("no_num");
    } else if (str.search(/[a-z]/) == -1) {
        alert("no lowercase letter");
        return ("no_lowercase_letter");
    } else if (str.search(/[A-Z]/) == -1) {
        alert("no uppercase letter");
        return ("no_uppercase_letter");
    } else if (str.search(/[@#$%]/) == -1) {
        alert("no_special_character");
        return ("no_special_character");
    }
    alert("ok!!");
    return ("ok");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.