Introduction


This post shows how to validate an email ID in Javascript or jQuery. Here is a Javascript function to validate the email ID, you need to add the class "email" in the text control.
Code
  1. <script type="text/javascript">
  2. $(document).ready(function () {
  3. $(".email").change(function () {
  4. var inputvalues = $(this).val();
  5. var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  6. if(!regex.test(inputvalues)){
  7. alert("invalid email id");
  8. return regex.test(inputvalues);
  9. }
  10. });
  11. });
  12. </script>
  13. Html input type text
  14. Email : <input type="text" class="email" >