0

I'm trying to implement password validation using regex or javascript. unfortunately i'm a total newbie to Regex :/

the criteria are :

  • minimum length: 8 characters (can be upper or lower case)
  • must contain 1 special character (something like !?$%&@)
  • must contain at least one number

i found the following snippet but it's missing checking for at least character of type special+number ..

function validPassword(password) {
    var has_letters = (/[a-zA-Z]/).test(password);
    var has_numbers = (/[0-9]/).test(password);
    var has_length = 3 <= password.length && password.length <= 30;
    return has_letters && has_numbers && has_length;
}

thanks

1

4 Answers 4

1

You can use below regex to validate your password:

var regex = "^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$";

This will check validation for Minimum 8 characters at least 1 Alphabet, 1 Number and 1 Special Character.

1

Another way would be

function validPassword(password) {

    return password.length > 8 
       && password.match( /[\d]/ ) 
        && password.split( /[\W]/ ).length == 2 ;
}

password.length > 8 checks the length should be minimum 8

password.match( /[\d]/ ) checks if it has at least one number

password.split( /[\W]/ ).length == 2 checks if it has one special character

2
  • Any line that makes you scroll horizontally to see it all is too damn long ;) Feb 22, 2016 at 11:18
  • @NiettheDarkAbsol hope its not too long now :) Feb 22, 2016 at 11:19
1

Your regex should be like this:

(?=^.{8,}$)(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^A-Za-z0-9]).*

Here is the detail:

(?=^.{6,}$) - String is > 5 chars
(?=.*[0-9]) - Contains a digit
(?=.*[A-Z]) - Contains an uppercase letter
(?=.*[a-z]) - Contains a lowercase letter
(?=.*[^A-Za-z0-9]) - A character not being alphanumeric.
1

Add one extra check:

function validPassword(password) {
    var has_letters = /[a-zA-Z]/.test(password);
    var has_numbers = /\d/.test(password);
    var has_special = /[!?$%&@]/.test(password);
    var has_length = (password.length >=8 && password.length <= 30);

    return has_letters && has_numbers && has_special && has_length;
}

However if you want a single regex to do all this then use lookaheads:

var re = /^(?=.*\d)(?=.*[!?$%&@])(?=.*?[a-zA-Z]).{8,30}$/;

var isValid = re.test(password);
4
  • i suppose it doesnt check for length=1 on special character and number(?)
    – Fuxi
    Feb 22, 2016 at 11:17
  • I believe you want at least one special character? Or is it that you want exact one special character?
    – anubhava
    Feb 22, 2016 at 11:18
  • 1
    @Fuxi you haven't said 'number should be exactly one', you said must contain at least one number Feb 22, 2016 at 11:18
  • 1
    @anubhava yes you were right, sorry for that .. at least number was correct
    – Fuxi
    Feb 22, 2016 at 11:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.