Depending on what "invalid" means, this could work:
def isValid(emails: String): Boolean = {
return emails.split(",").map(_.count(_ == '@') == 1).reduce(_ && _)
}
isValid("[email protected],[email protected],[email protected]") // true
isValid("[email protected],b@@gmail.com,[email protected]") // false
isValid("[email protected],[email protected]@gmail.com") // false
I'm just splitting the string by commas, then I check that there is only one @
in it.
If you really want to check that the email is correct, you can use a regex like this:
def isValid(emails: String): Boolean = {
return emails.split(",").map(_.matches(raw"[\w-\.]+@([\w-]+\.)+[\w-]{2,4}")).reduce(_ && _)
}
This is probably better, but it is not specific enough to capture all email specificities. Matching all types of emails is not something easy to do with regex (see this question).