0

I know there are so many repeated questions in stack but i didn't found any help, so please help me. Email should be like this,

"abc@abc.com;abc@abc.com; abc@abc.com ; abc@abc.com ;abc@abc.com;"

I also need code for validating this email string

I found this one but it is looking like incomplete Validate a comma separated email list

Thanks In advance

Community
  • 1
  • 1
Mahesh Kalyankar
  • 107
  • 1
  • 1
  • 13
  • **I also need code**: We need to to see your effort first. – anubhava Jun 16 '15 at 06:42
  • Why go all crazy, get a proper email validation, split on the ; trim the segments of the split and validate each. – Mathijs Segers Jun 16 '15 at 06:42
  • Something like: `validAddresses = []; list.split(/\s*;\s*/).forEach(function(s){/* build array of valid addresses */});` – RobG Jun 16 '15 at 06:43
  • 1
    You can split and parse the email addresses one by one to check that they appear valid but the only real validation is to send a confirmation email. – Xotic750 Jun 16 '15 at 07:17
  • If you want a regular expression that validates according to RFC822 grammar, see: [*Mail::RFC822::Address: regexp-based address validation*](http://ex-parrot.com/~pdw/Mail-RFC822-Address.html). – RobG Jun 16 '15 at 07:24
  • A RegExp is not really suitable for the job, better would be to use a parser ([example library](https://github.com/FogCreek/email-addresses)) then simply execute `var emailList = emailString.split(';').map(Function.prototype.call, String.prototype.trim).filter(emailAddresses.parseOneAddress);` to get a valid list in Array format. [jsFiddle example](http://jsfiddle.net/Xotic750/0ka9qdy9/) – Xotic750 Jun 16 '15 at 07:38
  • I am using this RegExp to validate my email /^[A-Z0-9\._%-]+@[A-Z0-9\.-]+\.[A-Z]{2,4}(?:[,;][A-Z0-9\._%-]+@[A-Z0-9\.-]+\.[A-Z]{2,4})*$/i; – Mahesh Kalyankar Jun 16 '15 at 09:17
  • it is not validating "abc@abc.com;abc@abc.com; abc@abc.com ; abc@abc.com ;abc@abc.com;" this string – Mahesh Kalyankar Jun 16 '15 at 09:18
  • `var stringOk = emailString.split(';').map(Function.prototype.call, String.prototype.trim).filter(Function.prototype.call, String.prototype.trim).every(emailAddresses.parseOneAddress);` – Xotic750 Jun 16 '15 at 12:36

3 Answers3

2

Divide et impera: split the string on semicolons and validate each email address.

Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64
2

This will return array of invalid emails:

function validateEmail(email) {
    var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    return re.test(email);
}


var str = "abc@abc.com;abc@abc.com; abc@a@bc.com ; abc@abc.com ;abc@abc.com;"

var emails = str.split(';');

var invalidEmails = [];

for (i = 0; i < emails.length; i++) { 
    if(!validateEmail(emails[i].trim())) {
      invalidEmails.push(emails[i].trim())
    }
}

alert(invalidEmails);

JsFiddle

Avinash
  • 6,064
  • 15
  • 62
  • 95
0

If you want to get only valid emails you can do it like this

function validateEmail(email) {
    var re = /([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)/ig;
    return email.match(re);
}

var valid = validateEmail("abc@abc.com;abc@abc.com; abc@abc.com ; abc@abc.com ;abc@abc.com; gfdgfdgdf");
console.log(valid);

this function will return you all email that are valid, and ignore all other inputs. Here is the fiddle example of it: http://jsfiddle.net/q6z4yv2z/

Aleksandar Gajic
  • 1,349
  • 1
  • 18
  • 23
  • The trouble with that is that it may simply trim invalid parts, e.g. `'foo#bar@bar.com'` will become `'foo@bar.com'`. More appropriate to use *split* and *test*, building an array from only those strings that return true. – RobG Jun 16 '15 at 07:09
  • It will ignore it as invalid, and return you only the others, yes it is better to split it but it is possible with regex to retrieve all valid ones :) – Aleksandar Gajic Jun 16 '15 at 07:14
  • No it doesn't. You don't consider the `;` in the regular expression so leading or trailing parts that don't match are ignored, provided the bit remaining matches. – RobG Jun 16 '15 at 07:17