Regex for validating multiple E-Mail-Addresses

后端 未结 10 1631
忘了有多久
忘了有多久 2020-12-01 00:32

I got a Regex that validates my mail-addresses like this:

([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]

相关标签:
10条回答
  • 2020-12-01 00:43

    Something I've written in my days. Basic email validation is taken from practical implementation of RFC 2822

    ^([a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])(;)?)+$
    

    matches emails with ; separator

    I you want more separators, swap (;)? with [;,|]? within the [] brackets.

    0 讨论(0)
  • 2020-12-01 00:46

    Why not just split on the semicolon and then validate each potential email address using your existing regexp ? Writing one huge regexp is going to be very difficult and a maintenance nightmare, I suspect.

    0 讨论(0)
  • 2020-12-01 00:48

    This is your original expression, changed so that it allows several emails separated by semicolon and (optionally) spaces besides the semicolon. It also allows a single email address that doesn't end in semicolon.

    This allows blank entries (no email addresses). You can replace the final * by + to require at least one address.

    (([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)(\s*;\s*|\s*$))*
    

    If you need to allow comma, apart from semicolon, you can change this group:

    (\s*;\s*|\s*$)
    

    by this one:

    (\s*(;|,)\s*|\s*$)
    

    Important note: as states in the comment by Martin, if there are additional text before or after the correct email address list, the validation will not fail. So it would work as an "email searcher". To make it work as a validator you need to add ^ at the beginning of the regex, and $ at the end. This will ensure that the expression matches all the text. So the full regex would be:

    ^(([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)(\s*;\s*|\s*$))*$
    

    You can add an extra \s* after the ^ to tolerate blanks at the beginning of the list, like this. I.e. include ^\s* instead of simply ^ The expression already tolerates blanks at the end as is.

    0 讨论(0)
  • 2020-12-01 00:56

    Below is my solution and it worked as expected for me:

          var emailReg = new RegExp(/^([A-Z0-9.%+-]+@@[A-Z0-9.-]+.[A-Z]{2,6})*([,;][\s]*([A-Z0-9.%+-]+@@[A-Z0-9.-]+.[A-Z]{2,6}))*$/i);
      var emailText = $('#email').val();
    
      if (!emailReg.test(emailText)) {
          alert('Wrong Email Address\Addresses format! Please reEnter correct format');
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 00:57

    Old post - needed the same RegEx. The accepted answer did not work for me, however, this did.

    ^(|([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$
    

    Retrieved from this post, however, the accepted answer did not work either, but the Regex on the link WITHIN the post did.

    abc@abc.com - validates

    abc@abc.com;123@qwer.com - validates

    abc@abc.com; - does not validate

    empty string - validates

    If you want to validate against an empty string, then remove the | at the beginning of the regex

    0 讨论(0)
  • 2020-12-01 01:02

    Domain names are actually way more complex. For example, most TLDs are now using Unicode domain names, which are quite common in Europe. Consider the email address mailtest@пример.испытание, which is actually perfectly valid (though they can always be transcribed to the old form - mailtest@xn--hxajbheg2az3al.xn--jxalpdlp). This means that it is probably easier to define what characters are not valid for a domain name. See ICANNs.

    In addition, the TLDs are also not strictly part of the set you have defined, see IANAs list of valid TLDs. For example, example@example.travel is a valid email address.

    In short, to validate email addresses, consider going with an established third party library, unless you are dealing with a limited special case.

    Now for your original question, I would recommend a preprocess stage where you split on reasonable delimiters (',' and ';'), trim whitespace (at least if dealing with user input) and validate each entry.

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