
This is a small, lightweight function for JavaScript that I use in conjunction with jQuery frequently. I call it, is_email. Here is the function, and if you know JS this will make sense right away:
function is_email(email){ var result = email.search(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,3})+$/); if(result > -1){ return true; } else { return false; } }
I use it with jQuery all the time. Here is an example on how to use it with jQuery:
$('#email_field').blur(function(){//When they tab/click out of the field... if(!is_email($(this).val())){//If the email field's value is NOT a valid email... $(this).removeClass('good').addClass('error'); //Remove "good" class if exists, and add error class } else{ $(this).removeClass('error').addClass('good'); //If it's a valid email, remove error class and add good class } });
You can view an example live at:
projectdeploy.org/contact.php