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 test@hotmail.com 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:

test@hotmail.com,test123@hotmail.com,....

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,}))(?:$|(?=,)))/;
halfer
  • 19,824
  • 17
  • 99
  • 186
Daredevil
  • 1,672
  • 3
  • 18
  • 47
  • 2
    Possible duplicate of [How to validate an email address in JavaScript?](https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript) – Alexander Lallier Apr 18 '19 at 12:17

2 Answers2

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('test@hotmail.com,test123@hotmail.com'));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
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>
Ajinkya
  • 325
  • 3
  • 12