6

I have to validate a email field which can contain multiple email address separated by (;). The following is the code i used

$("body").find(".reqEmail").filter(function(){
        var regex = new RegExp(/^[_A-Za-z0-9-]+[^(),:;<>\\[\\]@]*@[^(),:;<>\\[\\]@]*(\\.[A-Za-z]{2,})+$/);///^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        var email=$(this).val()
        if(regex.test(email)==false){
                e.preventDefault();
                $(this).css("border","solid 1px red");
                $(this).parent().find("#ReceAppEmail").html("Invalid Email!!");
                }
                else{return true;}
    });

It always give the error message, even i insert 1 email address. I cannot find where i went wrong. any suggestions?
FYI: This is included in the form submission (onsubmit)

Kuru
  • 109
  • 11

3 Answers3

3

You can grab all the email addresses separated by semicolons using the regex below:

/(?:((?:[\w-]+(?:\.[\w-]+)*)@(?:(?:[\w-]+\.)*\w[\w-]{0,66})\.(?:[a-z]{2,6}(?:\.[a-z]{2})?));*)/g

http://regexr.com/3b6al

rsnorman15
  • 510
  • 3
  • 8
  • awesome but i think OP do not want to get valid email, OP actiully wants to validate all emails which are separated by `,` – Dipesh Parmar Jun 11 '15 at 04:33
  • Yeah, you're are completely right. I answered before he added his code so it made sense at the time. – rsnorman15 Jun 11 '15 at 04:35
  • The above regex is slightly different from just validating an email address that it actually validates all emails separated by a semicolon too. The captured groups should be the individual emails. – rsnorman15 Jun 11 '15 at 04:42
1

You can do this by below code.

function validatecommSeptEmail(commSeptEmail)
{
    var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,5}$/;
    return (regex.test(commSeptEmail)) ? true : false;
}

function validateMultiplecommSeptEmails(emailcntl, seperator)
{
    var value = emailcntl.value;
    if (value != '') {
        var result = value.split(seperator);
        for (var i = 0; i < result.length; i++) {
            if (result[i] != '') {
                if (!validatecommSeptEmail(result[i])) {
                    emailcntl.focus();
                    alert('Please check, `' + result[i] + '` email addresses not valid!');
                    return false;
                }
            }
        }
    }
    return true;
}

How to use it?

onblur="validateMultiplecommSeptEmails(this,',');"
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
  • This answer also works, but i can't accept two answers. But the answer by @rsnorman15 is more closer for my requirement – Kuru Jun 11 '15 at 06:38
  • Well that's fine if you cant accept it, because SO is for helping programmer around the world, so it does not matter if you accept it or not, but we care more that we could help you. – Dipesh Parmar Jun 11 '15 at 06:40
0

Try this

function validateEmail(email) {
    var regixExp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    $(".reqEmail").css("border","solid 1px red");
    $("#ReceAppEmail").html("Invalid Email!!");
    return regixExp.test(email);
}
Zohaib Waqar
  • 1,204
  • 11
  • 18