Apps Script Basics – Form validation

In this post, we’re going to look at automatically setting up validation on a form. Validation allows us to control what the user inputs on the form, for example, to make sure they enter a number, make sure they write more than 10 words, etc.

Clocking in and out form

In the example below, we’re going to set up a simple clocking in and out form, which will require the employee to enter a 4-digit code. It will then add validation to the form to check that the code is valid. We’ll also make the questions required to make the user have to fill them out before submitting the form. Here’s the form:


The code

Either in a Google Sheet or Google Form, write the following code. We’re going to create a new form, create the employee number question, then add validation to it.

1.	function setUpValidation() {
2.	  //Set up form
3.	  const form = FormApp.create('Clocking in');
4.	  form.setTitle('Clocking in form');

Line 1: Set up the function.

Line 3: Create a new form using FormApp.create() and give it a name in the brackets.

Line 4: To give the form a title, get the form and set the title using setTitle().

Now, let’s add the employee number question.

6.	 //Set up page
7.	 const item = form.addTextItem()
8.	                  .setTitle('Employee Number')
9.	                  .setRequired(true);

Line 7: The question will require the employee to type in a number, so we need to add a text item. So, we get the form and use addTextItem(). Don’t add a semi-colon on the end, as we going to add extra parts to this. Store this in the variable called item, as we will need to refer to this when we add the validation.

Line 8: Give the question a title, using setTitle().

Line 9: Let’s ensure the employee fills it out by using setRequired() and in the brackets stating true. This makes it a required question. Add the semi-colon on the end.

Now, let’s add the validation to this question. The structure to this will be very similar for most types of validation. I.e. you create it, possibly set some help text, state the validation you want, then build it. At the end, you then need to set it to the question.

11.	  const textVal = FormApp.createTextValidation()
12.	     .setHelpText("Enter your 4 digit employee number.")
13.	     .requireNumberEqualTo(1234)
14.	     .build();
15.	  item.setValidation(textVal);

Line 11: Create the validation and choose the appropriate one for your question type. As we’re using a text item, we need to use createTextValidation(). Assign it to the variable textVal.

Line 12: We have the option of adding some help text, which will provide guidance to the user, as to what they need to enter. This is optional but is often a good idea. Use setHelpText() and write the help text in the brackets.

Line 13: Now, we add the validation we want. There are lots to choose from, but here we want the number typed in to be equal to their employee number, so we use requireNumberEqualTo() and in the brackets state the number it needs to equal.

Line 14: We then need to build our validation using build(), ending with a semi-colon.

Line 15: Finally, we need to add this validation to our question. We get the question, stored in the variable item, and use setValidation() and pass the variable textVal, which contains the validation, to it.

In the final part, we’re going to set up a simple multiple-choice question, with two options, ‘clock in’ or ‘clock out’.

17.	  form.addMultipleChoiceItem()
18.	      .setTitle('Clocking in or out?')
19.	      .setChoiceValues(["Clock in", "Clock out"])
20.	      .setRequired(true);
21.	}

Line 17: Add a multiple-choice item to the form. Here there’s no need to set up a variable as we’re not going to work with the question later on.

Line 18: Set up the question title.

Line 19: Set the options of the question, using setChoiceValues() and state the options in the brackets, using an array.

Line 20: We’ll also make this question a required one.

Line 21: Close the function.

Run the code (going through the usual authorization the first time) and you’ll find the form created in your My Drive.

Open the form and you’ll see the titles and questions that have been created.

Clicking on the employee number question, we can see the validation has been set up, so that it checks to see if the number entered equals ‘1234’. Note, it ignores the decimal point.

When the employee types in a number, if it’s wrong, they see the help text that we set up.

Whereas, if they type in the correct number, the help text disappears.


Other form validations

Here are the other validations you can set up:

You can find more information in the Google documentation here:

https://developers.google.com/apps-script/reference/forms/text-validation-builder


If you’re interested in setting up a clocking in and out system, check out my ‘clocking in and out system’ post.


This post is taken from my book “Beginner’s Guide to Google Apps Script 2 – Forms“, available on Amazon here.

Want to learn more about Google Workspace and Apps Script? The books below are available on Amazon. Just click on a book! (Affiliate links).

JavaScript Fundamentals for Apps Script users

a

Google Apps Script Projects 1
Google Apps Script Projects 2

6 comments

  1. Very nice article, I’ve really enjoyed reading it. Dear, is it possible to set validation rules in Google forms for short text question type by mathematical logical equation e.g. Input should satisfy input % 2 ==0 in addition that input shall be of certain digit length e.g. 6 digit number like 187234

    1. Hi-The length option only allows you to select a minimum or a maximum number. Probably the only way is to select the regular expression option… I would then Google the ref ex which will check for the property you want.

      1. Dear sir I do agree with you that regular expressions (regex) can provide great validation options that would otherwise difficult to do using other validations provided in google forms. For two rules I gave above (Input should satisfy input % 2 ==0 & 6 digit number like 187234), validation can be done by \d{5}[02468] or [0-9]{5}[02468] using regex. The issue is when for example validation is (input % 3 ==0) or other simple logical formula, then it will be difficult to use regex to set such validation alone (forget combination with other rules like digit length). I also believe that using Google Apps Script (GAS) does not allow for other validations other than those preset by Google validation methods. My question is there any work-around solution either using Google forms GAS alone or in combination with other services.

        1. Hi-I’m not aware of any workaround, as like you say GAS is limited to the validation methods found in the documentation. The input could be checked after submission but I’m guessing that isn’t what you’re after. The other way to do this would be to set up a HTML form where you could then use JavaScript to validate the inputs.

          1. Thanks sir for for your prompt reply. In fact I am just a starter but enthusiastic in the world of programming. I’ve googled my query and come the fact as you stated above that logic validation is not possible in google forms and it is only achievable through html form validation (many useful info comes from stackoverflow.com). So could you suggest some simple resources for novice about such html form implementation.

Comments are closed.