JavaScript – Regex Email Validation

In the below code I’m using JavaScript regex Email Validation method with Modern Browsers DOM Email Verification Method.

This code will first try to verify email by creating an email input DOM(For Modern Browsers) and if it fails, then it will user Regex Email Validation Method.

function ValidateEmail(email){

  var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

  var input = document.createElement('input');

  input.type = 'email';
  input.value = email;

  return typeof input.checkValidity == 'function' ? input.checkValidity() : re.test(email);

}

Verifying Email

You can use the above code like below to verify an email.

if(ValidateEmail("[email protected]")){
  
   //Email is verified
  
}else{

   //Invalid Email

}

You can use this code in email validation while submitting a form using Ajax.

Source 1, Source 2.

Shubham Kumar

Hey, I am Shubham and i love Blogging, Coding and exploring new things and obviously sharing my experience with you.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *