All free tools

Regex Generator for Form Validation

Pick a field type and get a ready-to-use regular expression, HTML pattern attribute, and JS snippet — then test it live against your own input.

100% in your browser — nothing you enter is uploaded

Practical email check — accepts plus-addressing and subdomains, rejects spaces and missing parts.

Regular expression
/^[^\s@]+@[^\s@]+\.[^\s@]+$/
HTML input
<input type="text" pattern="[^\s@]+@[^\s@]+\.[^\s@]+" required>
JavaScript
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!re.test(value)) {
  // value is invalid
}

The HTML example requires an answer. Remove required to allow an empty field. Pattern checks verify format; they do not prove a value is real or reachable.

Test it live
Matches the pattern
valid
invalid

Next: write an accessible error message or test a form before migration. HTML pattern reference.

What a validation pattern does

A validation pattern is a regular expression that describes the exact shape a field value is allowed to take. Attach one to an email, phone, postal code, or password field and the browser can reject malformed input before the form is ever submitted.

This generator gives you a tested regular expression for fourteen common field types, in three ready-to-paste formats: the raw regex, an HTML pattern attribute, and a JavaScript snippet.

Using the HTML pattern attribute

Copy the complete HTML input example to require a matching answer. Remove required to accept an empty field. Pattern works on text-like inputs, including text, tel, email and password; it does not apply to textarea or number inputs.

The pattern attribute is implicitly anchored, so the copy here drops the leading ^ and trailing $. For richer, real-time feedback, use the JavaScript form and test the value yourself as the user types.

Client-side validation is never enough

Regex validation in the browser gives immediate format feedback. A pattern attribute can be bypassed, so validate submitted values again on the server.

Keep error messages specific: identify the field and show an accepted example. A format match is not proof that an email can receive mail, a phone is assigned or a date exists.

Questions & answers

How do I use the HTML pattern attribute?+

Copy the HTML input example. The pattern checks the whole value, and required rejects an empty answer. Remove required if the field is optional. Modern browsers compile pattern using Unicode v mode; the generated patterns support that syntax.

Are these regular expressions strict or lenient?+

These are format checks with explicit limits. Email accepts plus-addressing but does not verify a mailbox. Phone and postal formats do not verify assigned numbers or addresses. The date pattern checks numeric ranges, not month lengths or leap years. Test your own accepted and rejected cases before use.

Should client-side validation be my only check?+

No. Regex validation in the browser is for fast feedback and good UX. Always validate again on the server — a pattern attribute can be bypassed.

Does anything get sent to a server?+

No. The patterns are a static library, and your test input is matched locally with the browser's own regex engine.

Make validation errors easy to fix.

See a working example that identifies the field, keeps the answer and explains the next step.