Search code examples
google-apps-scriptgoogle-formsgoogle-workspace

Validating email addresses belongs to our domain


I want to use a Google Form to populate a Google Spreadsheet. One of the fields is email address, and I need to validate this against the list of emails for our organisation - in other words forcing people to use valid and existing email addresses.

Our organisation uses Google Apps. The form will be created by a user who is in our organisation and only email addresses from our organisation/domain will be considered valid.


Solution

  • You can use the experimental Apps Script Domain Services API. Here's how I'd do it.

    function isValidEmailInMyDomain(address) {
      var parts = address.split('@');
      if( parts.length != 2 )
        return false;
      if( parts[1] != UserManager.getDomain() )
        return false;
      try {
        UserManager.getUser(parts[0]);
        return true;
      } catch(doesNotExist) {
        return false;
      }
    }
    
    function testFunction() { //check the menu View > Logs
      Logger.log(isValidEmailInMyDomain('aEmailIn@yourDomain.com'));
    }