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 & 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.


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.

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.

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.

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’.

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 authorisation 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.


You can find the above code on GitHub:


Other 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 below:

Apps Script – Clocking in & out system


eBooks available on Drive, Forms, Sheets, Docs, Slides, and Sheet Functions:

Baz Roberts (Google+Flipboard / Twitter)