Rohit Azad Rohit Azad - 6 months ago 18
Javascript Question

multiple email validate errro in if i used number

HI i m using to this code for multiple email validate .

ex. if i test to this email id

rohitazadkumw@gmail.com
,
rowkujerr@gmail.com
then it's working fine .

but if i used to this email id
rohit2345@gmail.com
,
rohit.azad2345@gmail.com
then it's show to error can u tell me where i m wrong this code .



var validEmailValue = [];
var nonValidEmailValue = [];

function checkEmail(email) {
var regExp = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{2,4})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
return regExp.test(email);
}


function checkEmails(textareaId) {
var textId = textareaId;
var emails = document.getElementById(textId).value;
emails = emails.replace(/\s+/g, '');
document.getElementById(textId).value = emails;
var emailArray = emails.split(",");
for (i = 0; i <= (emailArray.length - 1); i++) {
if (checkEmail(emailArray[i])) {
validEmailValue.push([emailArray[i]]);
} else {
nonValidEmailValue.push([emailArray[i]]);
}
}
}




$('#testEmailIDs').live('blur', function() {
validEmailValue = [];
nonValidEmailValue = [];
checkEmails('testEmailIDs');
if (nonValidEmailValue == '') {
alert('email Validate');
} else {
alert('not vallidate');
}
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<textarea id="testEmailIDs"></textarea>




Jai Jai
Answer

Your regular expression does not have numeric checks:

var regExp = /(^[a-z0-9]([a-z_\.0-9]*)@([a-z_\.]*)([.][a-z]{2,4})$)|(^[a-z0-9]([a-z_\.0-9]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;

var validEmailValue = [];
var nonValidEmailValue = [];

function checkEmail(email) {
  var regExp = /(^[a-z0-9]([a-z_\.0-9]*)@([a-z_\.]*)([.][a-z]{2,4})$)|(^[a-z0-9]([a-z_\.0-9]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
  return regExp.test(email);
}


function checkEmails(textareaId) {
  var textId = textareaId;
  var emails = document.getElementById(textId).value;
  emails = emails.replace(/\s+/g, '');
  document.getElementById(textId).value = emails;
  var emailArray = emails.split(",");
  for (i = 0; i <= (emailArray.length - 1); i++) {
    if (checkEmail(emailArray[i])) {
      validEmailValue.push([emailArray[i]]);
    } else {
      nonValidEmailValue.push([emailArray[i]]);
    }
  }
}




$('#testEmailIDs').live('blur', function() {
  validEmailValue = [];
  nonValidEmailValue = [];
  checkEmails('testEmailIDs');
  if (nonValidEmailValue == '') {
    alert('email Validate');
  } else {
    alert('not vallidate');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<textarea id="testEmailIDs" autofocus>aaa-123@gmail.com</textarea>