Question

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.

Was it helpful?

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'));
}

OTHER TIPS

Now you can put a Regular Expresion in the field:

For EMAIL:

[a-zA-Z0-9_\.\+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]+

Email Address – the regex below should match most common email address formats, including Gmail aliases that accept the “+” sign but there’s no perfect solution.

[a-zA-Z0-9_\.\+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]+

You can use like:

enter image description here

For More Regex: Useful Regular Expressions for Validating Input in Google Forms

Thank you.

This is not a Google Apps Script question, but when you create the form, you can select a check box that says "Automatically collect respondent's username. "

You don't need a script. Simply add a response validation to the question. In the first field, choose "text"; in the second field, "contains"; in the third field, "@'yourdomain'"- that's it!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top