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)

5
  • stackoverflow.com/questions/46155/… Commented Jun 11, 2015 at 4:26
  • Can you share the error message with us? Commented Jun 11, 2015 at 4:27
  • You need to var result = value.split(",") split them Commented Jun 11, 2015 at 4:28
  • You should split on the string with ";" then parse each one indiviually Commented Jun 11, 2015 at 4:28
  • The error message means i am getting the error which supposed to get when enters the invalid email @Stef Commented Jun 11, 2015 at 4:29

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

awesome but i think OP do not want to get valid email, OP actiully wants to validate all emails which are separated by ,
Yeah, you're are completely right. I answered before he added his code so it made sense at the time.
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.
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,',');"

2 Comments

This answer also works, but i can't accept two answers. But the answer by @rsnorman15 is more closer for my requirement
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.
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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.