Others



How to Validate email address using JavaScript Regular Expression


The following example shows, how to validate email address using JavaScript Regex(Regular Expression).

Regex
function emailValidation(mailTxt){
  var reg = /^([A-Za-z0-9._-])+\@([A-Za-z0-9._-])+\.([A-Za-z]{2,4})$/;
  return reg.test(mailTxt);
}
Example
<html>
  <body>
    <input type='email' id='mailInput'>
    <input type='button' value='Check' onclick='validate()'>

    <script>
    function validate(){
      txt=document.querySelector("#mailInput").value;
      if(emailValidation(txt)){
        alert("Valid Email Address");
      }else{
        alert("Invalid Email Address");
      }
    }

    function emailValidation(mailTxt){
      var reg = /^([A-Za-z0-9._-])+\@([A-Za-z0-9._-])+\.([A-Za-z]{2,4})$/;
      return reg.test(mailTxt);
    }
    </script>
  </body>
</html>
Try it Yourself

Demo