Practical guide · Updated

HTML pattern not working? Check the input and submit path

Debug an HTML pattern with a small test matrix: supported input types, full-value matching, empty fields and submission behavior.

The examples describe a fictional internal reference code, not a universal rule for names, phone numbers or postal addresses. Validate submitted data on the server as well.

Write the acceptance rule before the expression

For this example, a reference code must contain two uppercase ASCII letters followed by four digits, such as AB0042. Lowercase letters and surrounding spaces are deliberately rejected. That is a business rule for this example, not something to apply to ordinary names or international addresses.

Use the validation-pattern generator for a starting point, then test the markup that your form actually renders. A regular expression that works in a JavaScript test can still be attached to the wrong HTML control or bypassed by a custom submit flow.

Use a supported input and match the entire value

For the example, the relevant attributes are type="text", pattern="[A-Z]{2}[0-9]{4}" and required. Keep the explanation “Two uppercase letters followed by four digits, for example AB0042” visible next to the field.

The pattern attribute applies to text, search, url, tel, email and password inputs, not number inputs. HTML patterns match the entire value and are written without JavaScript slash delimiters. Browsers compile them with the Unicode v flag; an invalid expression does not impose the intended pattern constraint. Check escaping if you copy a more complicated character class. These behaviors are documented by MDN.

Keep a small regression matrix

Use the same cases in the actual browser form and in your server validation. If the results differ, record where the rule or normalization changed.

Keep a small regression matrix
ValueExpected outcome in this exampleReason
AB0042AcceptTwo uppercase letters and four digits
AB042RejectOnly three digits
ab0042RejectLowercase was not allowed by this example’s rule
AB0042XRejectAn extra character violates the full-value rule
“ AB0042 ” (spaces)RejectSpaces are not part of the accepted format
EmptyReject with required; otherwise not a pattern mismatchPresence and shape are different constraints

Check how the form submits

Try the normal submit button with an invalid value. Check whether the form has novalidate or the submit button has formnovalidate. These opt out of the normal interactive validation step. If code calls form.submit(), native constraint validation is bypassed; requestSubmit() follows the normal submission process.

If your application sends data with a custom handler, explicitly check validity and provide understandable errors before sending. Server-side validation remains necessary because browser checks can be bypassed. A pattern is a format check, not proof that an email address belongs to someone or that a reference code exists.

For the fictional code, decide whether surrounding whitespace should be rejected or trimmed. If the real workflow allows trimming, update both the test cases and server normalization deliberately. Do not silently change the pattern until an accidental input happens to pass.

Make the error useful

Explain the accepted format near the input and connect supporting text with aria-describedby. If your form displays a custom error, identify the field and the needed correction in text; color alone is insufficient. Keep the entered value available so the person can correct it.

A helpful example message is “Enter two uppercase letters and four digits, such as AB0042.” Avoid “Invalid input,” which gives no next step. Test keyboard navigation and submission on a phone as well as a desktop. The linked accessible-errors guide goes deeper into error summaries and field-level feedback.

Use the fix in the form you are migrating

Save the input attributes, test matrix and submit-path notes with the original form. They become acceptance criteria when recreating it elsewhere. SimilarForm helps teams recreate and review forms before publishing. Sign up if that migration workflow would save you recurring work.

Sources and reproducibility

Continue your task