0

I am struggling to create a Regex pattern that would validate multiple emails. I know that this topic has been widely discussed, however, having researched them all I could not find the answer to my specific question. My issue is as follows.

The project I am working on is written in PHP and uses FILTER_VALIDATE_EMAIL. I aimed at writing a front-end email validator to use for both single and multiple emails in a way, consistent with FILTER_VALIDATE_EMAIL. Here https://github.com/mpyw/FILTER_VALIDATE_EMAIL.js I have found the ideal Regex pattern compliant with latest RFC standard provisions, namely:

^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\.){1,126})+(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))]))$

Now I've been trying to make this particular pattern function for multiple emails. Here comes my question, how can I get it done? I was trying to set

[\s*,]*

in order to make multiple addresses pass, by so far all I got is a headache, so your assistance is highly welcome, thanks in advance!

guitarfreak
  • 89
  • 1
  • 8
  • @Wiktor Stribiżew This is not as duplicate. My topic mostly concerns Regexp and particularly the extension of possibilities of a pattern. Email validation goes second here. – guitarfreak Aug 16 '18 at 12:59

1 Answers1

1

Try this..

var pattern = /^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i

if(!pattern.test(inputYour))
{
  console.log('not a valid e-mail address');
}​

for multiple email..

var x = getEmails();
var emails = x.split(",");
emails.forEach(function (email) {
 validate(email.trim());
});

validate function should have your above code...

This is for check in one go...

var email = 'test@example.com, hello@example.com,mail@example.com';
alert ( (/^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i.test(email)) );

link https://www.experts-exchange.com/questions/28940315/JavaScript-REGEX-validate-multiple-emails-addresses-separated-my-commas.html

Avi
  • 1,424
  • 1
  • 11
  • 32
  • 1
    Not something I am looking to, my question mostly concerns the way I could extend functionality of the pattern I quoted in the initial message, though, thanks. – guitarfreak Aug 16 '18 at 12:43
  • As for the second block of code you added - yes, I've seen in one of SO topics, but again, I am not trying to validate every email via one pattern, but validate multiple emails. Indeed, most probably I will end up validating these one by one, however, I want to find the answer to the question I've been struggling with. – guitarfreak Aug 16 '18 at 12:46
  • Did you see this link https://www.regextester.com/99193 – Avi Aug 16 '18 at 12:50
  • Yes, this is what I'm looking for, or, better say, I am trying to liquidate a gap in my currently superficial knowledge of Regex. Thank you! – guitarfreak Aug 16 '18 at 12:50
  • As for the pattern you quoted - well, it does its work, however, I could not grasp the concept of ensuring the multitedness of my pattern. – guitarfreak Aug 16 '18 at 13:04
  • Hope it was useful for you and try to read about regex.. that would help you more to understand... – Avi Aug 16 '18 at 13:07
  • Finally, I managed to achieve the task - here is the email pattern for single email validation applied for multiple validatioN: – guitarfreak Aug 23 '18 at 11:05