Skip to content

Instantly share code, notes, and snippets.

@aliaspooryorik
Last active April 16, 2018 15:07
Show Gist options
  • Save aliaspooryorik/6fb8c067117b46037093a655650d0f96 to your computer and use it in GitHub Desktop.
Save aliaspooryorik/6fb8c067117b46037093a655650d0f96 to your computer and use it in GitHub Desktop.
validate email example
<cfscript>
input = [
'[email protected]',
'bar@locahost',
'bar"locahost'
];
foo = validateEmails(input);
writeDump(foo.hasErrors());
writeDump(foo.getErrors());
function validateEmails(emails) {
var errors = [];
var isValidEmail = function(email) {
return isValid("email", email);
// or if you want your own regexp
return reFind("^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$", email) > 0;
};
var registerError = function(email) {
errors.append("#email# is not valid");
};
// iterate and check the emails in the array
emails.each(function(email) {
if (!isValidEmail(email)) {
// log the error (as you do in your code)
registerError(email);
}
});
// methods you want to return
return {
"hasErrors": function() {
return arrayLen(errors) > 0;
},
"getErrors": function() {
return errors;
}
};
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment