2

I have a recipient textbox that allows to enter an email address and send to the person.As you know, there are regex to be considered when someone enters an email. For example, an email without @ is not an valid email.

So I wrote a function that checks the regex of an email like this:

//check email address
    function validateEmail(email) {
        var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        return re.test(email);
    }

So if I write [email protected] then this would be valid and test@@@hotmail.com would not be valid. Yes this works fine with no issue. However, in my textbox I am allowed to send to multiple recipients in this form:

[email protected],[email protected],....

So if I enter the above,it will treat it as invalid but seeing that I am bad in regex expression, is there a way to allow this to go through?

Edit:

This is how my regular expression looks like:

var re = /(?:^|,)((([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))(?:$|(?=,)))/;
1

2 Answers 2

3

At the beginning of the pattern, add a non-capturing group for , or the beginning of the string:

(?:^|,)

And at the end of the pattern, add a non-capturing group for the end of the string, or lookahead for ,:

(?:$|(?=,))

This will allow multiple matches on a single line, if separated by commas.

(?:^|,)(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))(?:$|(?=,))
^^^^^^^                                                                                                                                                       ^^^^^^^^^^^

https://regex101.com/r/Dzyfqt/1

If you not only want to validate but extract the emails too, this will sometimes include a leading comma, which is probably undesirable - to fix that, enclose everything after the initial non-capturing group in another group:

(?:^|,)((([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))(?:$|(?=,)))

and the email address alone will be in the first captured group.

If you want to check to see if the string contains comma-separated email addresses, and nothing other than comma-separated email addresses, you can use the first pattern and replace every matching email with the empty string, and check to see if the final resulting string is empty:

function validateEmail(email) {
  var re = /(?:^|,)(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))(?:$|(?=,))/g;
  return email.replace(re, '') === '';
}
console.log(validateEmail('[email protected],[email protected]'));

20
  • How does the entire thing look like? I am really bad at recognizing symbols
    – Daredevil
    Commented Apr 18, 2019 at 10:27
  • I see. So basically what it does, is it checks is there is a comma at the end of an email?
    – Daredevil
    Commented Apr 18, 2019 at 10:30
  • It will only validate an email if it starts (at the beginning of the string, or after a comma) and if it ends (at the end of the string, or right before another comma). Commented Apr 18, 2019 at 10:32
  • So it's will also check ,[email protected] as invalid right? What about [email protected],?
    – Daredevil
    Commented Apr 18, 2019 at 10:33
  • Tried your solution and key in [email protected],[email protected] but it still throws invalid.
    – Daredevil
    Commented Apr 18, 2019 at 10:35
0

Please Check with this Code :

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
 
</head>
  
<script>
 function sendmessage(){

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

&emsp;&emsp; To:<input type="email" style="font-size: 10pt;" size="70" id="recipient"><br><br>
  
  <div class="Send">
    <button type="button" style="font: 13px/1.231 Trebuchet MS;" onclick="sendmessage()"> Send </button>
</div>
  
</body>
</html>

Not the answer you're looking for? Browse other questions tagged or ask your own question.