Email validation regular expression in javascript

22-02-2017 email validation regex

The html code is

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <p>Enter email:</p>
  <input id='email'>
  <button type='submit' onclick='validateEmail()'>Check Email</button>
</form>

Now when the button is clicked then the JavaScript function validateEmail() will be called. The bellow code in this function. Using regular expressions is probably the best way.

function validateEmail(){
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var email = document.getElementById('email').value;

   if (reg.test(email) == false) 
   {
      alert("Invalid Email Address");
      return false;
   }
}

 

 

 

You might also like:

Convert Time 24 hrs to 12 hrs format In javascript
12-03-2017 date javascript 12hrs convert
How to find Angular Errors
06-03-2017 errors angularjs exceptionHandler
Filter for replace text in Angular Js
05-03-2017 filter replace angular js
How to get drop down value when drop down change using javascript
27-02-2017 onchange events dropdown javascript functions
Get Unique values from array/object in javascript
19-02-2017 javascript unique remove repeated values from array