Validate Multiple Emails Comma Separated with javascript

后端 未结 9 2078
无人及你
无人及你 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 06:03

    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.

    0 讨论(0)
  • 2021-01-12 06:04

    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=""  />
    
    0 讨论(0)
  • 2021-01-12 06:07

    Use var result = value.split(","). You end up with an array.

    0 讨论(0)
提交回复
热议问题