Email validation regular expression in javascript

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:

File Upload Using Angular Js and Node Js

28-05-2018 AngularJs Node js file upload multer module

How to search text in string using php ?

29-10-2017 php search string search

Time Sorting Ascending or Descending Order in Javascript

19-07-2017 Javascript ascending order descending order

How to insert bulk information into database in single query execution

18-03-2017 insert sql bulk database

How to convert html to pdf in Node js

12-03-2017 pdf convert html node js