How to implement a captcha for Google Forms

Is it possible to make a random captcha for a google form? You may search and find on google groups and other sources a hard no to this question but, in fact, there is one method that I made that works pretty well. Unfortunately, there are some catches with the method that I am about to show you, but until we get to that, we first have to ensure that we can meet a requirement that this method has.  This primary requirement is an URL/server that can generate dynamically a captcha image from a given text passed into an HTTP GET variable.

Luckily there are some free servers that can do just that. In this instance, I had used “”, but if you want you can use your own server as there are many examples of code for generating an image from an HTTP get variable available in many web programming languages.

Ok so, if we met this requirement, is time to do a somewhat detailed explaining of how this method will work. First of all is important to know that this method will use a google script, and regrettably, the captcha can’t be generated for every user request as google API does not allow execution of scripts when a client opens a form. So instead the captcha will be generated on a time-based trigger. In my example, I used two hours but google permits triggers to execute even after one minute. Just be careful to not exceed the resource quota allocated for your account.

So here is the google apps script:

function OpenForm() {
var text = "";
var lastIndexItem = 4;
var formId = '1MXfvfUjUM1L-phthM7oKv-teXK1ChisaEBcd0ekmLqA';  
var existingForm = FormApp.openById(formId);
  while(existingForm.getItems().length > lastIndexItem){

var strLen = 5;
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
// Make somthing random
for( var i=0; i < strLen; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));  
var img = UrlFetchApp.fetch(''+text);
// Add the iamge to the form
     .setHelpText('Verificare impotriva spam') // The help text is the image description
// Add the text Item
var textItem = existingForm.addTextItem().setTitle('Introduce textul de mai sus.').setRequired(true);
var textValidation = FormApp.createTextValidation()
   .setHelpText("Nu ai introdus corect textul de verificare") // Error message text if input is not corect.

As you can see from this script you must set some variables according to your needs:

  • strLen – number of characters displayed in the captcha
  • img –  URL for getting the image
  • lastIndexItem  – the last index of your form entry ( captcha will be added after this one)
  • formId – id of your form

After that set your trigger as you can see in this image.

The final result should look like this.

Is worth to notice that all data validation that is  made in google Forms have the answers present in plain javascript in the source code, so it can’t protect you from a smart bot, but it will help with most of them as I think no one will spend time to write a bot that passes your captcha even though is not very hard.




Share the joy

Leave a Reply