Skip to main content

JavaScript Regex Validation — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Regex Validation lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Accept digits only

    easy10 min

    Check the value is nothing but digits.

  2. 2

    Reject mixed input

    easy10 min

    Check a value with a letter is refused.

  3. 3

    Check a fixed format code

    easy10 min

    Accept three capitals followed by three digits.

  4. 4

    Allow an optional prefix

    easy10 min

    Accept the number with or without a leading plus.

  5. 5

    Combine with a range check

    medium10 min

    Check the shape with a pattern and the range with a number.

Find the bug(5)

  1. 1

    Fix the unanchored digit check

    easy8 min

    Without anchors a mixed value passes.

  2. 2

    Fix the case-sensitive code check

    easy8 min

    The code requires capitals, so lowercase must be refused.

  3. 3

    Fix the range checked by pattern

    easy8 min

    A pattern cannot express a numeric range, so check the number.

  4. 4

    Fix the plus treated as a quantifier

    easy8 min

    A literal plus must be escaped.

  5. 5

    Fix the empty value accepted

    easy8 min

    A star allows an empty string, which should be refused.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace