I want to validate a string which can be an email or multiple emails separated by commas.
For example:
bill.gates@hotmail.com -> TRUE
bill -> FALSE
In general it is not possible to validate email addresses. Something that is syntactically valid does not necessarily go anywhere, and even if it does whether it will be read.
And even then, covering all the possible syntactically correct addresses (according to the applicable RFC) needs an extremely complex regex (see 1st edition of "Mastering Regular Expressions" (Friedl, O'Reilly) for the 4724 or 6598 character versions, and these probably don't handle IDNs). E.g. an apostrophe is a valid character in the local part, and you can have comments inside the email.
I used validationEngine and underscore.js to do this:
function validateEmail(field, rules, i, options) {
if (!_.all(field.val().split(","), function(candidate) { return $.trim(candidate).match(options.allrules.email.regex); } ))
return options.allrules.email.alertText;
}
And then decorated my form field with:
<input class="validate[required,funcCall[validateEmail]]" type="text" name="contact_email" id="contact_email" value="" />
Use var result = value.split(",")
. You end up with an array.