Practical Validation Patterns
Build pragmatic patterns for emails, phone numbers, dates and IPv4 addresses, and learn where regex validation should stop and real parsing begin.
Welcome back!
Step 1 of 15Learn
Good enough beats perfect
Search the internet for "email regex" and you'll find patterns thousands of characters long that try to implement the full email standard. Don't use them. The official address syntax allows quoted local parts, comments and other oddities that almost nobody uses, and even a "perfect" pattern can't tell you whether the mailbox exists. The only real test is sending an email with a confirmation link.
What you want from a validation regex is a pragmatic check that catches typos and obviously wrong input without rejecting real users. For email, something like:
^[^\s@]+@[^\s@]+\.[^\s@]+$It says: some characters that are not spaces or @, then @, then more such characters, a dot, and more such characters. It accepts "ana.ruiz+news@mail.example.org" and rejects "ana@", "ana mail.com" and "ana@@x.com."
Overly strict patterns cause real harm. Rules like "the domain ending must be 2 to 4 letters" reject valid addresses on newer endings such as ".photography." Rules that forbid "+" break the plus-addressing many people use to filter mail. Rules that allow only [A-Za-z] in names reject "Zoë," "O'Brien" and "José." When in doubt, be permissive in the regex and verify by other means.
Step 2 of 15Practice · Diagram
The pragmatic email pattern
Which part rejects "ana@@mail.com"?
Tap the shape that answers the question, then press Check.
- ^[^\s@]+@[^\s@]+\.[^\s@]+$
- [^\s@]+ before @
The local part: anything except spaces and @.
- @ exactly once
The classes exclude @, so a second one fails.
- Domain name
Again no spaces or @.
- \. a literal dot
- Ending like com or org
At least one character after the last dot.
Step 3 of 15Practice · Sort into groups
Does each input pass the pragmatic email pattern ^[^\s@]+@[^\s@]+\.[^\s@]+$?
Tap an item, then tap its group (or drag it there).
Step 4 of 15Learn
Phone numbers and normalization
Phone numbers arrive in endless formats: "(555) 123-4567," "555.123.4567," "+1 555 123 4567," "5551234567." Writing one pattern to accept every format, and only valid ones, gets ugly fast.
A cleaner strategy is to normalize first, validate second:
- Remove everything except digits and a leading plus:
phone.replace(/[^\d+]/g, ""). - Validate the normalized form with a simple pattern. For US numbers with an optional country code:
^(\+1)?\d{10}$.
This splits a messy problem into two easy ones, and the normalized value is what you should store anyway, so searching and comparing numbers works.
If you do want a single pattern for a few known US formats, optional pieces handle it:
^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$It accepts "(555) 123-4567," "555-123-4567" and "5551234567." It also accepts the odd "(555123-4567," because the two parentheses are optional independently. That's the kind of small imperfection pragmatic patterns accept. For international numbers, rules vary so much by country that a dedicated library, such as Google's libphonenumber, ported to many languages, is the professional choice.
Step 5 of 15Diagram
Normalize first, validate second
Tap a shape with to see more.
- Raw: (555) 123-4567
- Strip all but digits and +
replace(/[^\d+]/g, "")
- Normalized: 5551234567
- Check ^(\+1)?\d{10}$
- Store the normalized form
Step 6 of 15Practice · Work it out
After running "+1 (555) 123-4567".replace(/[^\d+]/g, ""), how many characters long is the resulting string?
Step 7 of 15Learn
Dates: shape versus validity
^\d{4}-\d{2}-\d{2}$ checks a date's shape, but it happily accepts "2026-13-45." You can tighten the ranges with alternation:
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$Months are 01–09 or 10–12; days are 01–09, 10–29 or 30–31. This rejects month 13 and day 45. But it still accepts "2026-02-30" and "2026-04-31," and handling month lengths and leap years in a regex produces a monster that nobody can maintain.
This is the general rule: regex checks structure; code checks meaning. Use the pattern to confirm the format and extract the parts with groups, then let a date library or a few lines of code verify that the date exists:
const m = s.match(/^(\d{4})-(\d{2})-(\d{2})$/);
if (m) {
const d = new Date(Date.UTC(+m[1], +m[2] - 1, +m[3]));
const valid = d.getUTCMonth() === +m[2] - 1 && d.getUTCDate() === +m[3];
}The same split applies to numbers ("is this between 1 and 65,535?"), credit cards (format by regex, checksum by code) and many other values.
Step 8 of 15Practice · Select all that apply
Which inputs does ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ accept? Select all that apply.
Select all that apply.
Step 9 of 15Learn
IPv4 addresses
An IPv4 address is four numbers from 0 to 255 separated by dots, like 192.168.1.20. A first attempt, ^\d{1,3}(\.\d{1,3}){3}$, gets the shape right but accepts "999.300.1.1."
To restrict each part to 0–255 in pure regex, list the ranges digit by digit:
25[0-5]|2[0-4]\d|1\d\d|[1-9]?\dThat reads: 250–255, or 200–249, or 100–199, or 0–99 (with no leading zero for two-digit numbers). Wrapped in a non-capturing group and repeated with dots, the full pattern becomes long but correct:
^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$Notice the precedence detail: the alternatives are grouped so that the dot and anchors apply to all of them. Forgetting that group is the classic bug, as you saw with ^cat|dog$.
Alternatively, combine a simple pattern with code: match ^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$ and check that each captured number is at most 255. Both are acceptable; the second is easier to read and change. For real network code, many languages provide built-in address parsers that also handle IPv6.
Step 10 of 15Practice · Diagram
One IPv4 number, 0 to 255
Which branch matches 199?
Tap the shape that answers the question, then press Check.
- One octet
- 25[0-5]
250–255
- 2[0-4]\d
200–249
- 1\d\d
100–199
- [1-9]?\d
0–99, no leading zero
Step 11 of 15Note
Step 12 of 15Practice · Code challenge
Write isIPv4(text) that returns true only if the whole string is four numbers separated by dots, each number between 0 and 255 with no leading zeros (so "01" is invalid, but "0" is fine). You may use a single regex, or a regex to capture the four parts plus code to check the range.
isIPv4("192.168.1.20")isIPv4("255.255.255.255")isIPv4("0.0.0.0")isIPv4("256.1.1.1")isIPv4("10.0.0")isIPv4("10.01.0.1")isIPv4("1.2.3.4.5")
Step 13 of 15Practice · What would you do?
What would you do?
A teammate proposes validating customer names with ^[A-Za-z]+ [A-Za-z]+$ (two words of English letters). What is the main problem?
Step 14 of 15Note
Step 15 of 15Practice · Choose one
A form must accept dates only if they really exist (so "2026-02-30" is rejected). What is the most maintainable approach?
Lesson complete!