Check the value is nothing but digits.
Acceptance criteria
- 1.#out should show accepted.
JavaScript Tutorial · JavaScript Regular Expressions
These 10 tasks go with the JavaScript Regex Validation chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Regular expressions are commonly used to check the shape of input.” .
1.Accept digits onlyexercise · easy · 10 min
Check the value is nothing but digits.
Passes when: #out should show accepted.
2.Reject mixed inputexercise · easy · 10 min · practice plan
Check a value with a letter is refused.
Passes when: #out should show rejected.
3.Check a fixed format codeexercise · easy · 10 min · practice plan
Accept three capitals followed by three digits.
Passes when: #out should show accepted.
4.Allow an optional prefixexercise · easy · 10 min · practice plan
Accept the number with or without a leading plus.
Passes when: #out should show both accepted.
5.Combine with a range checkexercise · medium · 10 min · practice plan
Check the shape with a pattern and the range with a number.
Passes when: #out should show out of range.
6.Fix the unanchored digit checkbug fix · easy · 8 min
Without anchors a mixed value passes.
Passes when: #out should show rejected.
7.Fix the case-sensitive code checkbug fix · easy · 8 min · practice plan
The code requires capitals, so lowercase must be refused.
Passes when: #out should show rejected.
8.Fix the range checked by patternbug fix · easy · 8 min · practice plan
A pattern cannot express a numeric range, so check the number.
Passes when: #out should show out of range.
9.Fix the plus treated as a quantifierbug fix · easy · 8 min · practice plan
A literal plus must be escaped.
Passes when: #out should show both accepted.
10.Fix the empty value acceptedbug fix · easy · 8 min · practice plan
A star allows an empty string, which should be refused.
Passes when: #out should show rejected.
Between them these tasks cover Validation patterns, Anchoring and Combining checks.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.