Forms · Glossary

What is a regex pattern in a form field?

A regex pattern is a regular expression attached to a form field that the whole answer must match before it is accepted. It describes the shape of a valid answer, such as exactly four digits for an Australian postcode, and anything that does not fit that shape is rejected with a validation message.

Patterns catch typing slips that no other rule can, such as a letter in a postcode or a missing digit in a business number. They also cause some of the worst form errors on the web, because a pattern that is slightly too strict silently turns away real people with real answers.

· Co-founder

5 min read · Published

Five useful patterns for Australian forms
AnswerPatternAcceptsWatch out for
Postcode^\d{4}$2000, 0800Leading zeros, so store it as text rather than a number
ABN^\d{2} ?\d{3} ?\d{3} ?\d{3}$12 345 678 901, 12345678901Checks the 11 digit shape only, not whether the number is registered
Mobile number^(\+61|0)4\d{8}$ after removing spaces0412345678, +61412345678People type spaces and brackets, so tidy the value first
Order number^ORD-\d{6}$ORD-104233Case: ord-104233 fails unless the pattern allows lower case
Work email domain^[^@\s]+@tallowood\.com\.au$sam.lee@tallowood.com.auProves the domain was typed, not that the mailbox exists

How a browser reads a pattern

On a web form, a pattern is added to a text input through the pattern attribute, and MDN spells out the details that trip people up. It applies to text, tel, email, url, password and search inputs. It must match the entire value rather than part of it, as though the expression were wrapped in anchors at the start and the end, so a postcode pattern of four digits will not accept five. It is compiled with the v flag, which makes it Unicode aware. An empty value is not checked at all unless the field is also required, so a pattern alone never forces an answer. When a value fails, the field reports a pattern mismatch and the form will not submit until it is fixed.

Writing patterns people can pass

Start from real answers, not from the specification. Collect twenty genuine examples of the value, including the messy ones, and make sure every one of them passes before worrying about what the pattern rejects. Tidy the input before matching: remove spaces from phone and business numbers, trim the ends, and convert to upper case if that is how the value is stored. Then tell people the format in visible text near the field. MDN warns against relying on the title attribute for this because many browsers and screen readers do not expose it well, and the W3C forms tutorial shows the expected format written straight into the label. A pattern people can see is a pattern people can meet on the first try.

Where patterns cause harm

Some answers should never have a pattern. Names come in every length and script, with apostrophes, spaces and hyphens, and a pattern that allows only letters rejects a surprising share of real people. Street addresses are nearly as varied. Email addresses are the classic trap: a pattern that tries to follow every rule in the standard is unreadable and still wrong, which is why OWASP suggests a basic check and then letting the mail system decide. Badly built patterns are also a security risk. OWASP lists regular expression denial of service among the concerns, where nested repetition makes some inputs take so long to test that a server stalls, and recommends anchoring the whole string rather than using wildcards that match anything.

Patterns belong on the server too

A pattern in the browser is a convenience for the respondent, not a guarantee for the organisation. Anybody can remove the attribute from the page or send data straight to the address the form posts to, so every pattern that matters has to be checked again where the answer is received. OWASP describes this as allowlist validation: define exactly what good input looks like and reject everything else, on the server, using anchored expressions. The server version is also the right place for the checks a pattern cannot do, such as looking up whether a business number is registered or whether an order number exists, which turn a shape check into a real one.

Patterns and this form builder

This form builder has no regex pattern setting. Its Validation settings offer a Required Field switch, minimum and maximum length on short and long text, and minimum and maximum value on number fields, and neither the browser nor the server checks a pattern on any field. The practical substitutes cover most cases. A number field with a range handles quantities and amounts. Equal minimum and maximum lengths catch a postcode typed with three or five characters, although length is only checked when the form is submitted. A dropdown or radio field replaces any answer with a fixed set of values, such as a state. For a business number or an order reference, collect it as short text, as the trade account example does with its ABN, and check it in the system that receives the webhook.

Questions people ask

What does regex stand for?

Regex is short for regular expression, a compact notation for describing text patterns that most programming languages and browsers understand. A backslash d means any digit, curly brackets give a count, a caret marks the start and a dollar sign marks the end. The notation is powerful and easy to misread, so every pattern deserves a comment explaining what it is for.

Do I need to add the start and end anchors myself?

In an HTML pattern attribute, no: the browser treats the pattern as matching the whole value already. In server code, yes, almost always. Most programming languages match a pattern anywhere inside the text unless it is anchored, so a check for four digits would happily accept a value that contains four digits somewhere among other characters.

Why does my pattern reject real phone numbers?

Usually because people type spaces, brackets, dashes or a country code that the pattern did not expect. Strip everything except digits and a leading plus sign before matching, then test the tidied value. It is also worth asking whether the form needs a strict phone check at all, since a wrongly formatted number is often easier to fix by calling than by rejecting.

Should a postcode be a number field?

No. Postcodes, account numbers and phone numbers are identifiers made of digits, not quantities. A number field can drop leading zeros, shows increment arrows that make no sense, and may reformat long values. Use a text field, limit its length, and on mobile ask for a numeric keyboard so the respondent still gets the convenience of digits.

Can a pattern check that an ABN is valid?

Only its shape. A pattern can confirm there are 11 digits with optional spaces, but a correctly shaped number can still be mistyped or belong to nobody. Confirming that the number is real means checking it against the public register, which is a job for the receiving system rather than for the form field.

How should a pattern error message be worded?

Describe the format that works, not the failure. Enter four digits, for example 3000 tells the respondent exactly what to type, while invalid format tells them only that they got it wrong. Show the same wording as a hint before any error happens, so most people never see the message at all.

Make one with forms

The button opens the generator with this use case already described. Change the wording to match your own.

Create a form with OneCraft

Related questions

Step by step in the builder: Every form field and when to use it, then Automate your form with add-ons.

Sources

Written and checked by the OneCraft team. Last checked .