问题
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.
回答1:
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'));
}
回答2:
Now you can put a Regular Expresion in the field:
For EMAIL:
[a-zA-Z0-9_\.\+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]+
回答3:
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:
For More Regex: Useful Regular Expressions for Validating Input in Google Forms
Thank you.
回答4:
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. "
回答5:
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!
来源:https://stackoverflow.com/questions/10650164/validating-email-addresses-in-google-forms