Regex Email Validation Error - Using JavaScript

//if HTML5 input email input is not supported
if(type == 'email'){    
    if(!Modernizr.inputtypes.email){ 
         var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@([a-zA-Z0-9\-])+\.+([a-zA-Z0-9]{2,4})+$/;              
         if( !emailRegEx.test(value) ){ 
            this.focus();
            formok = false;
            errors.push(errorMessages.email + nameUC);
            return false;
        }
    }
}

This is my javascript regex for validating email format. But when I tried it myself, it does not show any errors for anyone .. @ .. It does not check .com or anything else at the end. What am I doing wrong?

0
source share
1 answer

You need to use a regular expression that really works for an email address. Your current one is completely broken, as there are many valid addresses that it will not accept.

See http://www.regular-expressions.info/email.html for a more detailed description and arguments on why a regular expression is not so good.

, , , , :

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

, regular-expressions.info :

RFC 2822, . - 99,99% .

+1

All Articles