4

Can we do regex pattern checking for password validation in reactJS?

Since I am new to reactJS, I need regex pattern for validating the password.

Following are the conditions for validating password.

a) Password should contains one Capital letter

b) It should start with special character @ or #

c) It should not contain any vowel a,e,i,o,u letters

d) It should be alphanumeric.

e) Length of password should be between range 8 to 14

2
  • I have no idea to try the above, since I have shortage in time for delivering other due to personal work. Commented May 16, 2017 at 5:23
  • agreed with @aryamccarthy Commented May 16, 2017 at 5:47

5 Answers 5

5

The simpliest way is to check all rules separately.

There's a function i wrote for you:

function password_validate(password) {
    var re = {
        'capital' : /[A-Z]/,
        'digit'   : /[0-9]/,
        'except'  : /[aeiou]/,
        'full'    : /^[@#][A-Za-z0-9]{7,13}$/
    };
    return re.capital .test(password) && 
           re.digit   .test(password) && 
          !re.except  .test(password) && 
           re.full    .test(password);
}

Or the same function in one line:

function password_validate(p) {
    return /[A-Z]/.test(p) && /[0-9]/.test(p) && !/[aeiou]/.test(p) && /^[@#][A-Za-z0-9]{7,13}$/.test(p);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Do no provide answers to users that doesn't show effort to solve the problem, SO isn't a code-writing website.
@PedroLobito, is it your personal position or there is some regulation rules on a site?
@PedroLobito, ok, this is personal position of group of people, includes you... it's not official position... so, do not impose it on me, please...
3

This regex will work :

^[@#](?=.{7,13}$)(?=\w{7,13})(?=[^aeiou_]{7,13})(?=.*[A-Z])(?=.*\d)

Explanation

^[@#] Starts with @ or #

Now we can add some conditions this way :

(?=condition)(?=condition)(?=condition)

This means "match condition but after that continue matching at the original match-point."

You can add as many conditions as you want, and this will be an "and."

(?=.{7,13}$) Length of password should be between range 8 to 14

(?=\w{7,13}) It should be alphanumeric.

(?=[^aeiou_]{7,13}) It should not contain any vowel a,e,i,o,u letters or underscore which is matched by \w

(?=.*[A-Z]) Password should contains a Capital letter

(?=.*\d) It should be alphanumeric so it should contain a digit

Demo

Comments

2
password_validate = (password) => {
    var re = {
        capital: /(?=.*[A-Z])/,
        length: /(?=.{7,40}$)/,
        specialChar: /[ -\/:-@\[-\`{-~]/,
        digit: /(?=.*[0-9])/,
    };
    return (
        re.capital.test(password) &&
        re.length.test(password) &&
        re.specialChar.test(password) &&
        re.digit.test(password)
    );
};
  1. /(?=.*[A-Z])/ - will check at least one capital char
  2. /(?=.{7,40}$)/ - will check the char length between 7 to 40
  3. /[ -/:-@[-`{-~]/ - will check at least one symbol
  4. /(?=.*[0-9])/ - will check at least one number

1 Comment

Please add some explanation rather than posting only code.
1

If you want to check all rules in one pass - try this formula:

^([@#](?=[^aeiou]{7,13}$)(?=[[:alnum:]]{7,13}$)(?=.*[A-Z]{1,}.*$).+)$

Demo

Comments

0

Standard one - /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})/.test(paswrd)

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.