Last active
April 16, 2018 15:07
-
-
Save aliaspooryorik/6fb8c067117b46037093a655650d0f96 to your computer and use it in GitHub Desktop.
validate email example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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