Skip to curriculum
Jump to the editor

2.8. Validate an email and a strong password with regular expressions

Medium
Topics

This question is in

2. Strings and text
20–30 min

Sign-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

JAVASCRIPT
function isValidEmail(email)          // boolean
function passwordProblems(password)   // an array of rule names
function isStrongPassword(password)   // boolean

Requirements

isValidEmail(email) is true when all of these hold, and false otherwise:

  1. email is a string of at most 254 characters, with exactly one @, and nothing else around it: no spaces and no line breaks anywhere.
  2. Before the @: 1 to 64 characters from A-Z a-z 0-9 . _ % + -. It does not start or end with a dot, and never has two dots in a row.
  3. After the @: two or more labels joined by single dots. Each label has 1 to 63 characters from A-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.
  4. 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:

  1. "length": fewer than 8 or more than 64 characters, counted as code points (an emoji is one).
  2. "lowercase": no letter a-z. "uppercase": no letter A-Z. "digit": no digit 0-9.
  3. "symbol": no character that is not an ASCII letter, a digit or whitespace. Whitespace is not a symbol; an emoji is.
  4. "space": contains any whitespace (space, tab, line break).
  5. It returns [] for a strong password and throws a TypeError for a value that is not a string.

isStrongPassword(password):

  1. is true exactly when passwordProblems(password) is empty, and throws the same TypeError.

Examples

JAVASCRIPT
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 only
JAVASCRIPT
passwordProblems("Tr0ub4dor&3");  // []
passwordProblems("abc");          // ["length", "uppercase", "digit", "symbol"]
passwordProblems("Abc def1!");    // ["space"]
isStrongPassword("password");     // false

Environment

  • 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

  1. A lookahead (?!…) checks a condition at a position without consuming any text. Where could one stop a leading dot, or two dots anywhere?
  2. What does a regular expression with the g flag remember between two calls to test?
  3. 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
Back to the question
Loading the editor…