Email Validation using JavaScript Regular Expression

Even now and then we need to add email validation on our forms. The following block of code provides you a way to perform email validation using JavaScript regular expressions.

<script>

var email1 = '[email protected]';

if(validateEmail(email1)) alert('Validated1 : '+email1);
else alert("Invalid email id1 : "+email1);

var email2 = '[email protected]';

if(validateEmail(email2)) alert('Validated2 : '+email2);
else alert("Invalid email id2 : "+email2);


function validateEmail(emailAddress){
        var pattern = new RegExp(/^[+a-zA-Z0-9._-][email protected][a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
        return pattern.test(emailAddress);
}
</script>

The validateEmail function returns true/false based upon the call performed using the JavaScript test function on Regular expression.

Good Luck!

Advertisements

Yuvraj has written 39 articles

"Learn & Share" - I believe in this. What about you?

  • Rahul Gupta

    Easy Code (Y) 🙂

  • Sanju

    Sometimes we stuck in situation where we don’t get what we really want. This code helped me to overcome the issue of email validation. It works great!!! Thanks.