Validate Google Forms responses with regular expressions
How regular expression response validation in Google Forms works, which patterns to use, how pasted and lookalike characters can fail validation, and when simpler options are better.
Google Forms lets you check a written answer against a regular expression before the form can be submitted. This guide covers what the check does and does not do to a respondent's text, how to write patterns that hold up when people type or paste their answers, and when a simpler setting or a plain instruction works better. No hands-on test results were available for this article. Pattern behavior described below follows standard regular expression logic, and anything that depends on the exact Google Forms implementation is flagged so you can confirm it in the form preview.
Is regex validation the right choice for your form?
Use it when answers must follow a fixed, predictable format, such as an internal ID, a postal code, a code with a known prefix, or an email address at one organization. The main benefit is that malformed answers are caught at entry, so you do not have to clean them up later in a spreadsheet. The main tradeoff is that a strict pattern also rejects valid answers that are written differently from what you expected, and the only explanation respondents see is your error message. Skip regex and accept plain text when real answers vary a lot, as with names, street addresses, international phone numbers or free comments, or when blocking a submission would cost you more than a messy entry.
What response validation actually changes
Validation is a gate, not a transformation. Google Forms does not reverse, restyle, mirror or substitute any characters. It checks the text the respondent entered against your rule and either accepts the answer or shows your error message and blocks submission until the answer passes. Validation does not reformat accepted answers. If you allow both '(555) 123-4567' and '555.123.4567', both appear in your results exactly as typed. Whether Forms trims leading or trailing spaces before checking or storing an answer is not confirmed by the available evidence. Test it in preview if your pattern depends on it.
To turn on validation, open a question, use the three-dot menu at the bottom of the question card, and choose Response validation. Short-answer questions offer number, text, length and regular expression rules. Paragraph questions offer length and regular expression rules. Within the regular expression option you pick a condition, enter a pattern, and can add custom error text.
- Contains: passes if the pattern is found anywhere in the answer.
- Doesn't contain: passes if the pattern is not found anywhere in the answer.
- Matches: intended to pass when the answer fits the pattern.
- Doesn't match: passes when the answer does not fit the pattern.
Matches versus contains, and why anchors help
The usual mistake is a pattern that finds a valid fragment inside an invalid answer. With Contains, the pattern [0-9]{5} accepts '123456789' because five digits appear inside it. Matches is generally described as checking the whole response, but the available evidence does not confirm that it behaves that way in every case. Adding ^ at the start and $ at the end removes the doubt. Anchors force the full answer to fit under either condition, and they do no harm if Matches already checks the whole string.
Google products generally use the RE2 regular expression engine. RE2 does not support lookaheads, lookbehinds or backreferences, so build patterns from plain character classes, groups, alternation and repetition counts. If you copy a pattern from a guide written for another engine, check it in preview before you publish.
Example patterns
Each pattern below assumes the Matches condition. Explicit ranges such as [0-9] and [A-Z] are used instead of shorthand classes because their meaning does not depend on how a given engine handles Unicode.
US ZIP code, 5 digits with optional +4:
^[0-9]{5}(-[0-9]{4})?$
ID of two uppercase letters followed by six digits:
^[A-Z]{2}[0-9]{6}$
US phone number, optional parentheses and separators:
^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$
Email at one organization (replace DOMAIN with your domain,
escaping each dot as \.):
^[^@ ]+@DOMAIN$
Yes or no, either capitalization of the first letter:
^([Yy]es|[Nn]o)$
The table shows the expected result for the ZIP pattern on sample inputs. These results follow from the pattern logic and were not recorded from a live form.
| Input | Expected result | Reason |
| 02139 | Pass | Exactly five ASCII digits |
| 02139-4307 | Pass | Optional hyphen and four digits present |
| 2139 | Fail | Only four digits |
| 02139 followed by a space | Fail unless Forms trims spaces | Anchored pattern does not allow a trailing space |
| 02139 written in fullwidth digits | Fail | Fullwidth digits are different characters from 0-9 |
Will pasted answers still pass?
This is where regex validation causes the most friction. People often paste answers from email, documents, PDFs or chat apps, and the pasted text can contain characters that look identical to what your pattern expects but are different code points. Common examples are non-breaking spaces, en or em dashes instead of hyphens, curly quotes, fullwidth digits and letters, and invisible characters such as zero-width spaces. Text styled with Unicode 'font' generators is a special case. It looks like bold, italic or script letters, but it is built from mathematical alphanumeric symbols, so it fails a class like [A-Za-z] even though a person reads it as ordinary letters.
Validation only accepts or rejects these characters. It does not convert them. If you widen a pattern to accept them, they end up in your stored results, where they can break sorting, matching and lookups in a spreadsheet. A strict pattern combined with an error message that tells people to type the answer rather than paste it usually gives cleaner data than a permissive pattern. If you expect pasting, accept common separator variants explicitly. For example, [-. ] already allows a hyphen, dot or space. Avoid broadening the pattern to match almost anything.
Readability and accessibility for respondents
Respondents never see your pattern. They see the question, its description and your error text. Put the required format and one concrete example in the question description so people get it right before they hit an error. Then write error text that says what was expected, such as 'Enter two capital letters followed by six digits, for example AB123456', rather than a generic 'Invalid answer'. If you leave the error text empty, respondents get no explanation of why their answer was rejected. How screen readers announce the validation error in Google Forms was not covered by the available evidence. Do not assume a particular behavior. If your audience relies on assistive technology, test the form with the tools they use.
When a simpler option is better
- Numeric ranges, such as an age between 18 and 99: use the Number validation type instead of encoding ranges in a regex.
- Any valid email address: use the Text option for email, or the form setting that collects respondent email addresses.
- Character limits: use Length validation.
- A small fixed set of answers: use multiple choice or a dropdown so nothing has to be validated.
- Names, addresses, international phone numbers and open feedback: accept plain text and clean up afterward, because any strict pattern will reject legitimate answers.
A safe workflow for adding a pattern
- Write down several valid and invalid sample answers, including variants you expect people to paste.
- Draft the pattern with ^ and $ anchors and explicit character ranges.
- Check the samples in a regex tester set to RE2 or a compatible flavor.
- Add the pattern in Google Forms with the Matches condition and clear error text.
- Open the form preview and submit each sample, including ones with trailing spaces or pasted punctuation.
- After the first real responses arrive, review them for rejected-but-valid formats people may have worked around, and adjust the pattern.
Open questions this guide could not confirm
The available evidence did not include documented or tested answers to several implementation details: whether Matches always requires a full-string match, whether surrounding whitespace is trimmed before validation, how the engine handles Unicode shorthand classes and inline flags such as case-insensitive mode, and how validation errors are announced to assistive technology. Each of these can change whether a specific answer passes, so confirm them in the preview of your own form rather than relying on any single description, including this one.