Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for validate password in ReactJS

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

like image 827
Sekar Raj Avatar asked Sep 16 '25 19:09

Sekar Raj


2 Answers

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);
}
like image 158
Sergey Khalitov Avatar answered Sep 18 '25 08:09

Sergey Khalitov


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

like image 39
Stephane Janicaud Avatar answered Sep 18 '25 08:09

Stephane Janicaud