Validating multiple email messages separated by javascript

I want to check a string, which can be email or email, separated by commas.

For example:

bill.gates@hotmail.com → TRUE
account → FALSE
bill.gates @ microsoft.com, steve.jobs @ apple.com "-> TRUE
bill.gates @ microsoft.com, steve.jobs @ apple.com, bob "-> false
bob, bill.gates @ microsoft.com, steve.jobs @ apple.com "-> false

I came out with the following code that works with test cases.

function validateEmail(field) { var regex=/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[AZ]{2,4}\b/i; return (regex.test(field)) ? true : false; } function validateMultipleEmailsCommaSeparated(value) { var result = value.split(","); for(var i = 0;i < result.length;i++) if(!validateEmail(result[i])) return false; return true; } 

Is this the fastest way to do this? What if I want to allow; and, as a delimiter?

+7
javascript regex
source share
9 answers

You cannot map top-level domains to [AZ]{2,4} :

What about .museum or .travel ? What about IDNA domains like .xn--0zwm56d or .xn--11b5bs3a9aj6g ?

In addition, it is perfectly true to have an email address such as "John Doe"@example.org .

In my opinion, all you have to check on the client side is that the address contains @ and if at least one after it . . On the server side, you can check if the server exists (you can even try connecting to the corresponding SMTP port) or just send a check mail.

Everything else is error prone, since the actual regular expression to check for valid email addresses is as follows.


Also this

 return (regex.test(field)) ? true : false; 

is serious WTF - test() already returns a boolean!


If you want to allow the use of different delimiters, use a string to separate instead of a regular expression, for example

 value.split(/,|;/) 
+8
source share

Use var result = value.split(",") . You will get an array.

+3
source share

Regex:

 ((\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*([,])*)* 
+2
source share

In general, it is not possible to verify email addresses. Something syntactically valid does not necessarily go anywhere, and even if it is executed, whether it will be read.

And even then, encompassing all possible syntactically correct addresses (in accordance with the applicable RFC), an extremely complex regular expression is required (see 1st edition of “Mastering Regular Expressions” (Friedl, O'Reilly) for versions with 4724 or 6598 characters, and they probably don't handle IDNs). For example. The apostrophe is a valid character in the local part, and you can have comments inside the email.

+1
source share

How about splitting a string in an array and scrolling it through one email address at a time?

0
source share

I used validationEngine and underscore.js for 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 the form field:

 <input class="validate[required,funcCall[validateEmail]]" type="text" name="contact_email" id="contact_email" value="" /> 
0
source share

I would select $.trim(result[i]) in this function validateMultipleEmailsCommaSeparated , as many people will add a space after the decimal point.

Of course, this is a jQuery thing, so the equivalent is your way.

0
source share

used this for php preg_match_all ()

 $pattern = '/([\w+\._%-]+@[\w+\.-]+\.[\w+]{2,4}[^,;\s])/'; $text = 'abc@efg.com, efg, hij@emg.com, ak@efg asfbd@efg.com ab@sdfs.com abc+efg@pqr.com'; preg_match_all($pattern, $text, $match); var_dump($match); 

 array(2) { [0] => array(5) { [0] => string(11) "abc@efg.com" [1] => string(11) "hij@emg.com" [2] => string(13) "asfbd@efg.com" [3] => string(11) "ab@sdfs.com" [4] => string(15) "abc+efg@pqr.com" } [1] => array(5) { [0] => string(11) "abc@efg.com" [1] => string(11) "hij@emg.com" [2] => string(13) "asfbd@efg.com" [3] => string(11) "ab@sdfs.com" [4] => string(15) "abc+efg@pqr.com" } } 
0
source share

try it

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

accepts both a comma and a semicolon as a separator, change this if you want only a comma as a separator

0
source share

All Articles