Hi I need help with a password regex
Requirements are 1 upper case letter, 1 lowercase letter and 1 number, 8-20 characters
Special characters allowed but not required.
Thanks!
Hi I need help with a password regex
Requirements are 1 upper case letter, 1 lowercase letter and 1 number, 8-20 characters
Special characters allowed but not required.
Thanks!
A regex doesn’t easily check for such things. It’s better to check for each of the requirements instead.
function validatePassword(password) {
return hasUppercase(password) &&
hasLowercase(password) &&
hasNumber(password) &&
password.length >= 8 &&
password.length <= 20;
}
Is it possible to use regex? I am using Jquery validate and have a .test function to populate
It’s possible to hammer in a nail using a hand saw, but it’s extremely ill-advised.
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$/gm
…?
This all hinges on how you hold your tongue when you say “easily”.
And, I can rest easy that Cunningham’s Law has come to the rescue.
Though to be fair, your answer isnt incorrect, the regex just does the same thing:
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$/gm
Regex delimiters and modifiers (strictly speaking the g is unnecessary.)
From the start of the string to the end:
There exists some digit.
There exists some lowercase letter.
There exists some uppercase letter.
There are between 8 and 20 characters in the string.
This is the power of Positive Lookaheads. (?=…) means “After this point, assert that the regex pattern “…” exists, and then return without consuming those characters.”
so when the … is .*[a-z] , the lookahead says “assert that there are 0-or-more characters, followed by a lowercase character, ahead of this point. Then forget that you’ve seen those characters, and move on.”
Thank you ever so much ---- again!
You rock star
Karen
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.