Validate answers in your form

With Google Forms, you can make sure that respondents fill out answers in accordance with guidelines that you set. For example, if you ask respondents for their email addresses, you can tell Google Forms to accept only email addresses that are properly formatted. 

Here’s how to set up validation:

  1. Create a form.
  2. From the Insert menu, select “Text,” “Paragraph text,” or “Checkboxes” from the list. (Note: These are currently the only question types that support data validation.)
  3. Click Data validation at the bottom of the question to show the data validation options.
  4. There are several ways Google Forms can validate form data. (These are described below, in the “Data validation settings” section.) Click the checkbox next to the validation types you’d like to enable, and adjust the options associated with each validation type.
  5. Respondents who enter invalid responses see an error message. You can modify this error message using the textbox on the far right.

Data validation settings

Currently, only “Text,” “Paragraph text,” and “Checkboxes” questions have support for validation. Each question type has its own validation settings.

Text

Text fields have three types of validation settings:

  • Number: Ensure the answer is a number of a certain type. For example, you can specify that the answer is a whole number or a number between 21 and 42.
  • Text: Ensure the answer is text with a certain property. For example, you can restrict answers to text containing the word “lollipop” or only accept email addresses.
  • Regular Expression: Ensure that the text contains or matches a certain regular expression. (Regular expressions are powerful, but require some know-how. See the “Regular expressions” below to learn more.)

Paragraph text

Paragraph text fields have two possible types of validation settings:

  • Text: Ensure that the answer can have up to or at least a certain number of characters. For example, you can restrict answers to have at most 100 characters.
  • Regular Expression: Ensure that the text contains or matches a certain regular expression. (Regular expressions are powerful, but require some know-how. See the “Regular expressions” below to learn more.)

Checkboxes

Checkboxes have two possible types of validation settings:

  • Select at least: Ensure that at least a certain number of checkboxes are checked in the answer.
  • Select at most: Ensure that at most a certain number of checkboxes are checked in the answer.
  • Select exactly: Ensure that exactly a certain number of checkboxes are checked in the answer.

Regular Expressions

Regular expressions provide a way to identify certain types of text, including particular characters, numbers, words, or patterns of characters. Regular expressions are particularly useful in pattern matching, as these searches are not restricted to a specific search term. Instead, searches return patterns that match the expression specified.

To use regular expressions in data validation for "Text" and "Paragraph text," select the Regular expression option in the validation settings.

Terms used in regular expressions

The table below shows a sample of just some of the expressions that Google Docs supports. There are, however, many other supported expressions users can employ.
Expression Description Example Matches Does not match
. A period signifies any character in the given position. d. do, dog, dg, ads fog, jog
* An asterisk after a character signifies a search for that preceding character repeated 0 or more times. do*g dog, dg, dooog dOg, doug
+ A plus after a character signifies a search for that character displayed 1 or more times. do+g dog, dooog dg, dOg, doug
? The previous expression is optional. do?g dg, dog dOg, doug
^ A caret must be placed at the beginning of a regular expression and signifies that the string starts with the character(s) or sequence placed after the caret. ^[dh]og dog, hog A dog, his hog
$ A dollar sign must be placed at the end of a regular expression and signifies that the string ends with the character(s) or sequence placed before the dollar sign. [dh]og$ dog, hog, hot dog dogs, hog, doggy
{A, B} The previous expression is repeated between A and B times, where A and B are numbers. d(o{1,2})g dog, doog dg, dooog, dOg
[x], [xa], [xa5] A character set indicates that just one of the given character(s) should occur in the current position. For the most part, any characters are valid within brackets, including characters mentioned previously in expressions: [xa,$5Gg.] d[ou]g dog, dug dg, dOg, dooog
[a-z] A character set range signifies a search for a character within the given range of characters. Common ranges include a-z, A-Z, and 0-9. Ranges can be combined into a single range: [a-zA-Z0-9]. Ranges can also be combined with character sets (mentioned previously): [a-zA-Z,&*]. d[o-u]g dog, dug, dpg, drg dg, dOg, dag
[^a-fDEF] A character set beginning with a ^ signifies a search for a character that is not within the given set. d[^aeu]g dog, dOg, dig, d$g dg, dag, deg, dug
\s Any white space character. d\sg d g, d[TAB]g dg, dog, doug

NOTE: When trying to search for actual instances of any character that has a specific meaning in regular expressions, like ^ and $, you need to "escape" the character in your search query by placing a backslash in front of it. For example, if you wanted to search for an instance of the $ character, you'd write \$.

Below are a couple of examples of how regular expressions could be used to search a spreadsheet:

Search for cells that contain dollar amounts

Enter the following in the Find bar: ^\$([0-9,]+)?[.][0-9]+

This signifies a dollar amount where the first number is any number 0-9 or comma occurring zero or more times, followed by [.], followed by any number 0-9 repeated one or more times. This search would return any of the following: $4.666, $17.86, $7.76, $.54, $900,001.00, $523,877,231.56

Search for cells containing US zip codes

Enter the following into the Find bar: [0-9]{5}(-[0-9]{4})?

This signifies a U.S. zip code consisting of five numbers with an optional hyphen and four-digit add-on.

Search for cells containing names beginning with a lowercase letter

Enter the following into the Find bar: ^[a-z].*

This signifies a cell input that contains a lowercase letter followed by another character 0 or more times. This search would return any of the following: bob, jim, gEORGE, marTin