How to validate email address in JavaScript

 
Regular expressions are a right way to validate text fields such as email addresses, phone numbers, names, age, date and other input information.

A regular expression can easily check whether a user entered something that looks like a valid email address.
Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec and test methods.
 
In the following example user enters a email address. When the user presses Enter, than JavaScript checks that email address is valid or not. If the number is valid then alert message will be displayed as ‘Email address is valid’. If the number is invalid, alert message will be displayed as ‘Email address is not valid.
 
For this we use this regular expression:
/[a-zA-Z0-9_-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/

<html>  
  <head>  
      <script type="text/javascript">  
      var reg = /[a-zA-Z0-9_-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/;  
      function EmailValidation(email)
      {  
        var OK = reg.exec(email.value);  
        if (!OK)  
          window.alert("Email Address isn't  valid");  
        else  
          window.alert("Email Address  is  valid");  
      }  
    </script>  
  </head>  
 
  <body>  
    <p>Enter your email address and then press Enter.</p>  
    <form action="">  
      <input name="txtEmail" onchange="EmailValidation(this);">  
    </form>  
  </body>  
</html>