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
bill.gates@microsoft.com,steve.jobs@apple.com" -> TRUE
bill.gates@microsoft.com,steve.jobs@apple.com, bob" -> false
bob,bill.gates@microsoft.com,steve.jobs@apple.com" -> 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?