Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email Validation in jQuery using RegExp

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)

like image 636
Kuru Avatar asked Jun 11 '15 04:06

Kuru


People also ask

How to validate email ID using regex in jQuery?

Email validation in jQuery using regular expression is most important when you get real email id. We will create form and click button to validate email id. If email is invalid then display error message, otherwise display success message using regex in jQuery.

How to do email validation of a textbox?

You can also use jQuery validate plugin, which enables to do email validation of a textbox if implemented with form We would require extra JS plugin to be used with jQuery validate for email validation, you can include it in your page using this link So the sample to validate form and email would look like this

How to validate any email address in HTML5?

Another simple solution would be here to use HTML 5 "pattern" attribute to validate any email. In the above HTML form, we have included pattern attribute, which has email pattern similar to regex solution one and then we can validate email address using it. You can modify the pattern based on your need.

How does a regex check if there is a mailserver?

a regex alone on clientside, does not know if there is a mailserver nor if the domain itself exists. it merly checks if the syntax of any email is valid or not. Its only to help the user writing the correct address. its not a validation.


2 Answers

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

like image 118
rsnorman15 Avatar answered Oct 08 '22 05:10

rsnorman15


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,',');"
like image 33
Dipesh Parmar Avatar answered Oct 08 '22 04:10

Dipesh Parmar