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
- <script type="text/javascript">
- $(document).ready(function () {
-
- $(".email").change(function () {
- var inputvalues = $(this).val();
- var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
- if(!regex.test(inputvalues)){
- alert("invalid email id");
- return regex.test(inputvalues);
- }
- });
-
- });
- </script>
-
- Html input type text
- Email : <input type="text" class="email" >