Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to validate email address in javascript?
I have below function for validating email address

C#
function validateEmail(txtEmail) {
    //var a = document.getElementById(txtEmail).value;

    var filter = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{0,4}$/;
    if (filter.test(txtEmail)) {
        return true;
    }
    else {
        return false;
    }

}


but it is not validating if i enter abc@xyz.com.com for this case it should return false currently it is returning true
Posted
Comments
bbirajdar 19-Mar-12 9:10am    
You need not correct it.. The validation is correct. It should validate the email ids like bbirajdar@yahoo.co.in ..with double (.) dots in the domain.. right ??
Sergey Alexandrovich Kryukov 19-Mar-12 20:25pm    
So what? ...com.com is unusual but not formally invalid.
--SA

http://www.regular-expressions.info/email.html[^] has a regex for validating an email address. There is also so comments on the shortcomings of this expression.
\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
 
Share this answer
 
try:
C#
function validateEmail(elementValue){
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   return emailPattern.test(elementValue);
 }


but i think nothing is wrong with your input too.
 
Share this answer
 
v2
Comments
Syed Salman Raza Zaidi 19-Mar-12 8:35am    
my pattern is fine but it is accepting dual .com ie abc@xyz.com.com
member60 19-Mar-12 8:37am    
check if in place of .com if you entered anything else such as.xyz then also it will treat in same way for both .com and .xyz
Syed Salman Raza Zaidi 19-Mar-12 8:39am    
still validating .com.com :(
try this

C#
function chkEmail() {
            //testing regular expression
            var a = $("#email").val();
            var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
            //if it's valid email
            if ( filter.test( a ) ) {
               alert('coorect');
                return false;
            }
            else {
                alert( 'wrong' );
                return false;
            }
        }
 
Share this answer
 
Comments
Syed Salman Raza Zaidi 19-Mar-12 9:17am    
still validating .com.com

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900