Validate Multiple Emails Comma Separated with javascript

后端 未结 9 2127
无人及你
无人及你 2021-01-12 05:03

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

9条回答
  •  难免孤独
    2021-01-12 05:49

    try this

         function validateEmails(string) {
            var regex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
            var result = string.replace(/\s/g, "").split(/,|;/);        
            for(var i = 0;i < result.length;i++) {
                if(!regex.test(result[i])) {
                    return false;
                }
            }       
            return true;
        }
    

    accepts both comma and semicolon as separator, change that if you want only comma as separator

提交回复
热议问题