Validate email address using jQuery
Earlier I had posted about Validate Date format using jQuery, Validate Date using jQuery and Validate Phone numbers using jQuery. And in this post, find jQuery way to Validate email address.
The validateEmail() functions (created below) will accept a parameter which is nothing but the email address and using regex it validates the email address. If it is correct then it returns true, otherwise false.
function validateEmail(sEmail) { var filter = /^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$/; if (filter.test(sEmail)) { return true; } else { return false; } }?
One just need to make a call to this function to validate the email address. For demo, I have used on click event of button. But It can be used on blur event of textbox or any another event.
$(document).ready(function() { $('#btnValidate').click(function() { var sEmail = $('#txtEmail').val(); if ($.trim(sEmail).length == 0) { alert('Please enter valid email address'); e.preventDefault(); } if (validateEmail(sEmail)) { alert('Email is valid'); } else { alert('Invalid Email Address'); e.preventDefault(); } }); });
See result below.
Feel free to contact me for any help related to jQuery, I will gladly help you.