Home Text Tools Math & Numbers Developer Tools Date & Time Image Tools Blog
Regex Tester

Test Regex Patterns for Email, Phone and Postal Code Fields

Build and test validation patterns for email, phone and postal code inputs, and learn where regex helps and where it should stop.

A form field that accepts anything invites bad data, and one that rejects valid input annoys real users. Regular expressions sit in the middle, and the way to get them right is to test them against many realistic examples before they reach production.

Regex Tester FREE online toolRuns in your browser. No sign-up, nothing to install. Open the tool

Start with a test list

Write down strings that should pass and strings that should fail before you touch the pattern. For an email field include a plain address, one with a plus sign, one with a subdomain, and one with a dot in the local part. Add failures such as a missing @, spaces, a double @ and a missing domain.

Paste all of them into the Regex Tester, one per line, and check the match list and count (add the m flag if you anchor each line with ^ and $). A pattern that looks right but has never met a bad example is not tested.

Save that list. When someone reports a valid address that was rejected, add it to the list, fix the pattern, and re-run everything. Over time the list becomes a regression suite that protects you from breaking old cases while fixing new ones.

Build the pattern

Check what each group holds before you rely on it. The tester lists whole matches only, so if you plan to extract the area code or the ZIP extension, test that part as a pattern of its own and confirm the groups in your code, long before a wrong group causes a bug.

  1. Remember the tester uses JavaScript regular expressions, so double-check any syntax your own language handles differently, such as Python for a backend check.
  2. Start with anchors ^ and $ so the whole value must match, not just a piece of it.
  3. Add character classes and quantifiers one part at a time, watching the match list update.
  4. Test the passing list, then the failing list.
  5. Add the cases that surprised you to the list.
  6. Copy the final pattern and use it in code, remembering to escape backslashes correctly for your language.

Practical patterns

Postal codes are the best fit for regex because the format is fixed. A US ZIP code is five digits, optionally followed by a hyphen and four more, which is ^\d{5}(-\d{4})?$. A UK postcode has a more complex structure, so a simpler check with a lenient pattern is often better than a strict one that rejects real codes.

Phone numbers vary widely between countries, so a rigid pattern will block people. A friendlier approach is to strip spaces, hyphens and parentheses first and check that what remains is digits, with an optional leading plus sign and a length of about 7 to 15 digits, which follows the E.164 maximum.

  • Anchor every validation pattern with ^ and $.
  • Be generous with email: a basic shape check is enough.
  • Normalize input (trim, remove separators) before matching.
  • Use named test cases so you can re-run them when the pattern changes.

Know the limits

A regex can tell you whether a string looks like an email, not whether the mailbox exists. The only reliable check is to send a confirmation message. An overly strict pattern will reject valid addresses, such as those with plus signs or newer top-level domains, so a short check like one @ with something on each side is often the right choice.

Beware of catastrophic backtracking. Nested quantifiers such as (a+)+ can make a pattern take very long on a crafted input and slow down your server. Keep patterns simple, and avoid running complicated expressions on untrusted strings of unbounded length.

Always validate on the server as well. A browser-side pattern improves the user experience, but it can be bypassed, so treat it as a convenience and not as a security measure.

For identifiers such as national IDs, order numbers or invoice codes, the format is usually documented by whoever issues them. Follow that definition exactly, including length and allowed letters, and add check digits only if you can verify them properly. Otherwise, a plain shape check is the safer choice.

Try the free Regex Tester
Runs in your browser. No sign-up, nothing to install.

Comments (0)

No comments yet. Be the first to share a thought.