Wednesday, March 28, 2012

jQuery Validate Email Address

This is how to validate an email address.

html:
<form method="post">
<input type="text" id="email"/>
<input type="submit" val="submit" id="submit">
</form>

jQuery:
<script type="text/javascript">
jQuery('#submit').click(function() {
var hasError = false;
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var emailaddressVal=jQuery('#email').val();
if(!emailReg.test(emailaddressVal)||emailaddressVal=='') {  
    alert('invalid');
    return false;
}
else{
    alert('valid');
    return false;
    }
});
</script>
Hope this will help!..

No comments:

Post a Comment