๐Ÿ› ๏ธ Developer ยท Updated November 21, 2026 ยท 8 min read

Form Validation Regex for ZIP Codes, Phones and Passwords

user input valid โœ“ โœ…

A good form validation regex is anchored with ^ and $, checks one rule at a time, and rejects only what is truly invalid, because every false rejection is a customer who gives up on your checkout or sign-up page. Build it in GrabCast's Regex Tester with a list of real inputs, turn on the m flag so each line is validated separately, and watch which lines match. This guide builds the fields that US web forms ask for most often: five-digit and ZIP+4 codes, ten-digit phone numbers typed a dozen different ways, password rules written with lookaheads, and email addresses. For each one you get a working pattern, the inputs it should accept and reject, and the honest limit where a regex stops being the right tool.

๐Ÿงช Try the Regex Tester tool now โ€” freeOpen โ†’
Phone pattern with an optional +1 country code in the pattern field and 3 matches highlighted: +1 415-555-0132, (415) 555-0199 and 415.555.0110
Add one rule at a time, such as the optional +1 country code, and re-check the matches.
๐Ÿ’ก Why validation rules make or break conversions

Validation sits directly in the path of revenue. Baymard Institute's checkout research repeatedly finds that overly strict fields, such as phone inputs that reject parentheses or name fields that reject apostrophes, cause abandoned orders. Loose rules have the opposite cost: unusable addresses, bounced emails and support tickets. Regular expressions are the natural tool for format checks, but they are also easy to over-tighten. Building each rule separately and running it against a realistic list keeps both failure modes visible.

Anchor every validation regex

A search pattern finds a match anywhere; a validator must match the whole value. The ZIP pattern in the tool's Common patterns menu uses word boundaries, \b\d{5}(?:-\d{4})?\b, which is right for finding ZIP codes inside an address block. For a form input, swap the boundaries for anchors: ^\d{5}(?:-\d{4})?$.

US phone numbers and ZIP codes, rule by rule

Phone fields attract the widest range of input: (415) 555-0132, 415.555.0132, 415 555 0132, 4155550132 and +1 415-555-0132 are all the same number. The library's US phone pattern accepts the first four, and adding an optional country code handles the fifth: ^(?:\+?1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$.

Password rules with lookaheads

Lookaheads let one expression check several independent conditions. The rule ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{12,64}$ requires a small letter, a capital, a digit and a length between 12 and 64. The Explain tab lists each lookahead in plain English, which is handy when a teammate asks what the rule enforces.

For testing, create sample passwords with the Password Generator, and see the Password Breach Checker for why a rule alone does not make a password safe.

Emails and names: validate lightly

Some fields should barely be validated. The library's email pattern, [\w.+-]+@[\w-]+\.[\w.-]+, checks for something, an at sign, a domain and a dot, which catches typos without rejecting real addresses. The only reliable proof that an address works is a confirmation message.

Step-by-step

1234
1Open the Regex Tester and paste one candidate value per line: valid examples first, then invalid ones, including typical typos from your own support tickets.
Test string with eight form entries, valid and invalid ZIP codes such as 02134-1234 and 9021, one value per line
Paste one candidate value per line: valid examples first, then typical typos.
2Start from the matching entry in the Common patterns menu, replace the word boundaries with ^ and $, and switch on the m flag.
ZIP pattern ^\d{5}(?:-\d{4})?$ anchored with caret and dollar, with the m flag switched on and the flag label reading gm
Start from the ZIP entry, swap the word boundaries for ^ and $, and switch on the m flag.
3Add one rule at a time, such as the optional country code or a lookahead for a digit, and confirm after each change which lines match.
Highlighted ZIP matches showing 90210, 02134-1234 and 10001 matched while 9021, ABCDE and 12345-12 stay unhighlighted
Check which lines match after the change.
4Copy the finished expression from the Code tab into your front-end code and your server-side validator, and keep the sample as a unit test.
Phone pattern with an optional +1 country code in the pattern field and 3 matches highlighted: +1 415-555-0132, (415) 555-0199 and 415.555.0110
Add one rule at a time, such as the optional +1 country code, and re-check the matches.

Common mistakes to avoid

โš ๏ธForgetting anchors, so a value with junk around a valid core passes validation.
โš ๏ธRejecting phone numbers with parentheses, dots or a leading +1, which blocks real customers at checkout.
โš ๏ธRestricting name fields to English letters, which fails for accented names, apostrophes and hyphenated surnames.
โš ๏ธTrusting browser validation alone, when any request can be sent to the server without the form.

Pro tips

โœ“Normalize before validating: trim spaces, strip phone punctuation and lowercase email domains, then check a simpler pattern.
โœ“Write error messages that name the rule, such as ZIP codes need five digits, rather than a generic invalid input.
โœ“Keep a spreadsheet of real rejected inputs from your logs and add them to the sample every quarter.
โœ“Validate on blur, not on every keystroke, so people are not scolded halfway through typing.
โœ“Share the pattern with teammates using the share link, and paste your sample list into the ticket, since the link does not include the sample.

Frequently asked questions

What regex validates a US ZIP code?

For a form field, use ^\d{5}(?:-\d{4})?$, which accepts 90210 and 90210-1234 and rejects everything else. Drop the ^ and $ inside an HTML pattern attribute, which anchors automatically.

Should I validate email addresses with a strict regex?

No. Strict patterns reject valid addresses. Check for a basic shape, something at a domain with a dot, and confirm the address with a verification email.

How do I require a digit and a capital in a password?

Use lookaheads: ^(?=.*[A-Z])(?=.*\d).{12,64}$. Each (?=...) checks one condition without consuming characters. Consider whether length alone serves your users better, as NIST guidance suggests.

Why does my pattern work in the tester but not in the HTML pattern attribute?

Browsers compile the pattern attribute with Unicode-aware flags, which reject some escapes that plain JavaScript tolerates, such as an escaped hyphen in the wrong place. Remove unnecessary backslashes and check the browser console for errors.

Can a regex check whether a phone number or ZIP really exists?

No. A regex only checks format. Use an address verification or phone lookup service when you need to know a value is real and reachable.

๐Ÿ“Œ Bottom line

Anchor the pattern, add one rule at a time, and check it against real inputs, especially the awkward ones from your support inbox. Keep phone and name rules forgiving, keep email checks light, and repeat every rule on the server. The Regex Tester's Common patterns, Explain tab and Code tab get you there quickly.

Open the Regex Tester tool โ†’

Related guides

Browse more: all text and developer guides ยท the Regex Tester tool