title: How to make a custom validation function like requireTextMatchesPattern(pattern) function using Google apps script?
body: <h1>Final Objective</h1>
  
  <p>The final objective is to have the ability to define custom validator for Google Forms item like <a href="https://developers.google.com/apps-script/reference/forms/text-validation-builder#requiretextmatchespatternpattern" rel="nofollow noreferrer">requireTextMatchesPattern(pattern)</a>.</p>
  
  <h1>Use Case</h1>
  
  <p>This custom validator will be used for example to compare what the user enters in the form item field with more than one value. or at least to have a custom functionality to execute when the user enters not valid data in the field.</p>
  
  <h1>Example</h1>
  
  <p>I have 3 participants, I want to make a simple authentication mechanism to make sure that the targeted audiences are going to participate. I have a spreadsheet that contains 3 passwords. The first question in the Form will require the user to enter a password. If the password doesn't match with one of the stored passwords in the spreadsheet, then, a validation message will appear to the user.</p>
  
  <h1>Partially Solution</h1>
  
  <p>Based on <a href="https://stackoverflow.com/questions/60434607/how-to-make-a-simple-authentication-in-google-forms-using-google-apps-script">this question</a> we can make a simple validation using <code>requireTextMatchesPattern</code> validator or directly from UI. The problem is that this validator limits the compare values to one.</p>
  
  <pre><code>function validatePassword() {
    // Create new custom form
    var form = FormApp.create('New Form');
    var ss = SpreadsheetApp.openById('SHEETID');
    var password = ss.getSheetByName('SHEETNAME').getRange('A1').getValue();
  
    // Create first question to check the password, it must be required so that the user does not have access to the rest
    // of the form if failed to log in
    var item = form.addTextItem().setRequired(true);
    item.setTitle('TEST TITLE');
  
    // Create validation for this question matching the password that we got from the sheet
    var textValidation = FormApp.createTextValidation()
      .setHelpText('You must enter the right password')
      .requireTextMatchesPattern(password)
      .build();
    item.setValidation(textValidation);
  }
  </code></pre>
  
  <p>What I am trying to do is to replace the <code>.requireTextMatchesPattern(password)</code> with a call to a custom validation function that does some validation process and then returns the type of <a href="https://developers.google.com/apps-script/reference/forms/text-validation-builder" rel="nofollow noreferrer">TextValidationBuilder</a>.</p>
  
  <h1>Research</h1>
  
  <p>I found <a href="https://github.com/DefinitelyTyped/DefinitelyTyped/blob/1febfb278c9f230ba6d3fc885986f7284fb8c6d6/types/google-apps-script/google-apps-script.forms.d.ts#L996" rel="nofollow noreferrer">this source code</a> which defines an interface of <code>TextValidationBuilder</code>. I don't know if it is the key to accomplish the main objective.</p>
  
  <p>Thanks!</p>