2.8. Validate an email and a strong password with regular expressions
Topics
This question is in
2. Strings and textSign-up forms check two things before they send anything: that the email address looks like one, and that the password is strong enough. Both are pattern checks, and both are classic uses of regular expressions. They are also where regular expressions go wrong: a pattern that accepts ana..lee@example.com, rejects ana+news@example.com, or answers differently on the second call.
Write three functions in validate.js: isValidEmail, passwordProblems and isStrongPassword.
Signature
function isValidEmail(email) // boolean
function passwordProblems(password) // an array of rule names
function isStrongPassword(password) // booleanRequirements
isValidEmail(email) is true when all of these hold, and false otherwise:
emailis a string of at most 254 characters, with exactly one@, and nothing else around it: no spaces and no line breaks anywhere.- Before the
@: 1 to 64 characters fromA-Z a-z 0-9 . _ % + -. It does not start or end with a dot, and never has two dots in a row. - After the
@: two or more labels joined by single dots. Each label has 1 to 63 characters fromA-Z a-z 0-9 -and does not start or end with a hyphen. The last label has two or more letters and nothing else. - A value that is not a string gives
false, and calling the function twice with the same address gives the same answer.
passwordProblems(password) returns the names of the rules the password breaks, in this order:
"length": fewer than 8 or more than 64 characters, counted as code points (an emoji is one)."lowercase": no lettera-z."uppercase": no letterA-Z."digit": no digit0-9."symbol": no character that is not an ASCII letter, a digit or whitespace. Whitespace is not a symbol; an emoji is."space": contains any whitespace (space, tab, line break).- It returns
[]for a strong password and throws aTypeErrorfor a value that is not a string.
isStrongPassword(password):
- is
trueexactly whenpasswordProblems(password)is empty, and throws the sameTypeError.
Examples
isValidEmail("ana+news@mail.example.org"); // true
isValidEmail("ana..lee@example.com"); // false: two dots in a row
isValidEmail("ana@-example.com"); // false: a label starts with "-"
isValidEmail("ana@localhost"); // false: one label onlypasswordProblems("Tr0ub4dor&3"); // []
passwordProblems("abc"); // ["length", "uppercase", "digit", "symbol"]
passwordProblems("Abc def1!"); // ["space"]
isStrongPassword("password"); // falseEnvironment
- Plain JavaScript in a browser page; the tests call the three globals from
validate.js. - These are the rules of this problem, simpler than the full email standard: quoted names, IP-address domains and non-ASCII addresses are out of scope.
- Any built-in may be used, and you may combine a regular expression with ordinary checks such as a length test.
Hints
- A lookahead
(?!…)checks a condition at a position without consuming any text. Where could one stop a leading dot, or two dots anywhere? - What does a regular expression with the
gflag remember between two calls totest? - Would one pattern for every password rule tell you which rule failed?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it