Email, URL and phone number fields are where bad data enters a system. A regex cannot fix that on its own, but a well tested format pattern catches the common accidents: the missing @, the space in the middle, the number that is a digit short. The trick is to test the pattern properly before trusting it.

This guide gives you one worked pattern for each of the three jobs, shows how to test each one in the free Regex Tester on Ovatly with strings that pass and strings that fail, and is plain about the limit of the whole approach: a regex checks shape, not existence.

Contents

  • Quick answer
  • What validation with a regex really means
  • The email pattern
  • The URL pattern
  • The phone number pattern
  • How to test a validation pattern, step by step
  • Limitations and alternatives
  • Common mistakes
  • FAQs
  • Conclusion

Quick answer

Use a simple anchored pattern per job: ^[^\s@]+@[^\s@]+\.[^\s@]+$ for email shape, ^https?://[^\s/]+\.[^\s]+$ for a web URL with a scheme, and ^\d{3}-\d{3}-\d{4}$ for one chosen phone format. Paste each pattern into the Regex Tester without slashes, add the g and m flags, and test it against strings that should pass and near misses that should fail. Remember the limit: these patterns check the shape of a string only. They cannot prove an email account exists or that a number is in service.

What validation with a regex really means

A validation pattern answers one narrow question: does this string have the characters in the places this format requires? It says nothing about the world behind the string. [email protected] passes an email pattern because it is shaped like an address; whether anyone receives mail there is a different question, answered only by sending a confirmation message.

That narrowness is a feature when you use it honestly. Format checks are fast, private and good at catching typos at the moment of entry. They fail you only when you ask them to do more: to prove ownership, deliverability or truth. Keep the pattern simple enough to explain, and let other steps, confirmation links, verification codes, lookup services, handle existence.

The email pattern

Pattern: ^[^\s@]+@[^\s@]+\.[^\s@]+$

Read it in pieces. The ^ and $ anchor it to the whole string. [^\s@]+ means one or more characters that are neither whitespace nor @, which forms the local part, then a literal @, then more non-space, non-@ characters, a literal dot \., and a final run to the end.

Strings that should pass: [email protected], [email protected]. Strings that should fail: ada@example (no dot after the @), ada example.com (a space, and no @), ada@@example.com (the second @ has nowhere legal to sit).

The tester earns its keep with groups here too. The entry example pattern (\w+)@(\w+)\.com against [email protected] shows group 1 as ada and group 2 as example, which is how you confirm a pattern not only accepts the address but splits it where you expect. Keep the validation pattern simpler than the parsing one; they are different jobs.

The URL pattern

Pattern: ^https?://[^\s/]+\.[^\s]+$

This requires the string to start with http:// or https://, then a host containing at least one dot, then any non-space characters for the path. It is deliberately a shape check for web addresses a user types into a form.

Strings that should pass: https://example.com, https://example.com/page?x=1. Strings that should fail: example.com/page (no scheme, so it fails at the start anchor), https://example (no dot in the host), https://exam ple.com (a space, which the pattern forbids everywhere).

If your form accepts addresses without a scheme, this pattern is the wrong tool and will reject good input; that is a requirements decision, not a regex problem. Decide what shapes you accept first, then write the pattern to match that decision, then test both lists.

The phone number pattern

Pattern: ^\d{3}-\d{3}-\d{4}$

Phone numbers have no single world format, so the honest approach is to pick the format your form promises and check exactly that. This pattern accepts one common dashed layout: three digits, a dash, three digits, a dash, four digits. The entry's own style example, \d{3}-\d{4}, is the same idea in shorter form.

Strings that should pass: 202-555-0147. Strings that should fail: 2025550147 (right digits, no dashes, so the shape is wrong for this format), 202-555-014 (a digit short), 202-555-01478 (a digit too many, caught by the end anchor).

If your users type numbers with spaces, brackets or a country code, write the pattern for that shape instead, and expect to maintain a small set of patterns rather than one monster that tries to accept everything and ends up accepting junk too.

How to test a validation pattern, step by step

  1. Open the Regex Tester and type the pattern in the Pattern field, without slashes.
  2. Put g and m in the Flags field. With m, the ^ and $ anchors apply per line, so one pasted list tests many candidates at once.
  3. Paste your candidate strings, one per line: the passes first, then the near misses.
  4. Read the match list. Matched lines passed; their indexes show where each starts. A line that is missing from the list failed, and the piece-by-piece reading of the pattern tells you why.
  5. Adjust the pattern one piece at a time and re-run, until the pass list and the fail list are exactly the ones you wrote down before testing.

Limitations and alternatives

Shape is all a regex checks. It cannot prove an email account exists, a domain accepts mail, a URL resolves to a real page, or a phone number is in service. Well shaped nonsense passes every pattern in this guide. For existence, use the channel itself: a confirmation email, a verification code by text message, or a lookup service.

The tester uses JavaScript's RegExp engine, and flavours differ slightly between JavaScript, PCRE, Python and grep, so re-test a pattern in the engine you ship. Results in the tester stop at 500 matches, which is plenty for format work but makes it a sampling tool, not a bulk processor. And be careful with very long patterns on long text: nested quantifiers can cause catastrophic backtracking and make the browser tab busy, which is one more reason to keep validation patterns short and plain.

Common mistakes

  • Trying to write the one perfect email pattern. The full official address grammar is enormous and still only checks shape. A short pattern plus a confirmation email beats a monster pattern alone.
  • Testing only the passes. A pattern that accepts everything passes all your good examples. The fail list is where validation earns its name.
  • Skipping the anchors. Without ^ and $, a pattern finds a valid shape hiding inside a longer junk string and reports a match.
  • One pattern for every country's phone numbers. Formats differ; promise one shape per form, or normalise the input first.
  • Treating a pass as proof. A passed format check means the string is shaped correctly. Nothing more, and building sign-up flows as if it meant more is how fake accounts sail through.

FAQs

Can a regex prove an email address is real? No. A regex checks only the shape of the string: characters in the right places. It cannot prove the mailbox exists, that the domain accepts mail, or that a phone number is in service. Only sending a message or a confirmation code proves that.

Why did my pattern pass an address that turned out to be bad? Because the address was well shaped. A string like [email protected] can satisfy every character rule while pointing at nothing real. Format validation filters typos and junk; it is not proof of existence.

Which flags do I need to test a list of candidate strings? Use g so every match is listed, and m so the ^ and $ anchors in an anchored pattern work at line boundaries. Paste one candidate per line and the match list shows which lines passed, with their indexes.

Why does a pattern behave differently in my code than in the tester? The tester uses JavaScript's RegExp engine, whose rules differ slightly from PCRE, Python or grep flavours. Re-test the pattern in the flavour you ship, especially character classes and escaping.

Is the text I test with uploaded anywhere? No. The Regex Tester runs in your browser on your device, so the addresses and numbers you paste stay local.

Conclusion

Pick the shape you accept, write the shortest pattern that checks it, and prove the pattern against both lists before you ship it. The Regex Tester makes that proof quick and private. If the testing workflow itself is new to you, start with How to Test a Regular Expression Online, and find more tools like this in the Developer Tools guides.