Password regex not working fine -
i'm using
function validatepassword(password) { var re = /^(?=.*[a-za-z])(?=.*[$@.$!%*#?&0-9])[a-za-z\d$@.$!%*#?&]{0,100}$/i; return re.test(password); }
for password contains @ least 1 numbers or special character it's working fine atleast 1 character if no character returns false though give number or special character
the /^(?=.*[a-za-z])(?=.*[$@.$!%*#?&0-9])[a-za-z\d$@.$!%*#?&]{8,15}$/i
matches
^
- start of string(?=.*[a-za-z])
- requires @ least 1 ascii letter in line(?=.*[$@.$!%*#?&0-9])
- requires digit or special symbols.[a-za-z\d$@.$!%*#?&]{8,15}
- 8 15 letters, digits , specified special symbols$
- end of string/i
- case insensitive matching
the (?=.*[a-za-z])
lookahead requires letter. if not need that, remove lookahead.
use
/^(?=.*[$@.!%*#?&0-9])[a-za-z\d@.$!%*#?&]{8,15}$/i
or - if password must contain atleast 1 number , 1 special character:
/^(?=[^0-9]*[0-9])(?=[^$@.!%*#?&]*[$@.!%*#?&])[a-z0-9@.$!%*#?&]{8,15}$/i
where (?=.*[$@.!%*#?&0-9])
split 2 lookaheads: (?=[^0-9]*[0-9])
requires @ least 1 digit , (?=[^$@.!%*#?&]*[$@.!%*#?&])
requires @ least 1 special symbol specified set.
demo:
function validatepassword(password) { var re = /^(?=[^0-9]*[0-9])(?=[^$@.!%*#?&]*[$@.!%*#?&])[a-z0-9@.$!%*#?&]{8,15}$/i; return re.test(password); } console.log(validatepassword("password")); console.log(validatepassword("12345678")); console.log(validatepassword("12345678morewords")); console.log(validatepassword("12468word!"));
Comments
Post a Comment