Skip to content

Instantly share code, notes, and snippets.

@jdspiral
Created October 20, 2015 17:32
Show Gist options
  • Save jdspiral/eea87ce0b74371b6c52e to your computer and use it in GitHub Desktop.
Save jdspiral/eea87ce0b74371b6c52e to your computer and use it in GitHub Desktop.
If email field is filled out, make sure it is validated
jQuery(document).ready( function() {
jQuery('.refer form').submit( function(){
return validateForm(this);
});
});
function validateForm(form){
var v = jQuery(form).find('.validate');
v.each( function(){
if( jQuery(this).val() != '' && jQuery(this).hasClass('email') ){
if( validateEmail(jQuery(this).val()) ){
jQuery(this).removeClass('error');
}else{
jQuery(this).addClass('error');
}
}else{
jQuery(this).removeClass('error');
}
});
if( jQuery(form).find('.error').length > 0 ){
return false;
}else{
return true;
}
}
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment