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
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);
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With