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:

Use java script file client side and server side at a time in node js
30-07-2017 node js include call javascript
How to submit form using javascript or jquery.
16-04-2017 javascript jquery submit form
How to use http-post request in node js
14-03-2017 http-post module node js
Sanitization in java sript
14-03-2017 sanitization java script
How to set or update or push the values into Object in javacript
27-02-2017 javascript set update push object