I want to validate a string which can be an email or multiple emails separated by commas.
For example:
[email protected] -> TRUE
bill -> FALSE
[email protected],[email protected]" -> TRUE
[email protected],[email protected], bob" -> false
bob,[email protected],[email protected]" -> false
I came out with the next code which works with the test cases.
function validateEmail(field) {
var regex=/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i;
return (regex.test(field)) ? true : false;
}
function validateMultipleEmailsCommaSeparated(value) {
var result = value.split(",");
for(var i = 0;i < result.length;i++)
if(!validateEmail(result[i]))
return false;
return true;
}
Is this the quickest way to do it? What if I want to allow ; and , as separator?