$(document).ready(function(){ /* INPUT FOCUS */ $('.site-form input,.site-form textarea,.site-form select').focus(function(){ $(this).addClass('focus'); }); $('.site-form input,.site-form textarea,.site-form select').blur(function(){ $(this).removeClass('focus'); }); /* FORM SUBMITTED TO SERVER */ $('.site-form abbr.form-error').each(function(){ label = $(this).parent(); //grab label object id = label.attr('for'); input = $('#'+id); //grab input object input_match = $('[title='+input.attr('title')+']').not(input); if($(this).hasClass('nonempty')){ validateEmptyFields(true,input); } if($(this).hasClass('match')){ validateMatchingFields(true,input,input_match); } if($(this).hasClass('email')){ validateEmailAddress(true,input); } }); //close each }); /*VALIDATE FORM*/ function validateForm(form){ resetFormErrors(); var errors = false; var label = ''; var input = ''; //cycle required fields $('.site-form abbr.required').each(function(){ label = $(this).parent(); //grab label object id = label.attr('for'); input = $('#'+id); //grab input object input_match = $('[title='+input.attr('title')+']').not(input); if($(this).hasClass('nonempty')){ errors = validateEmptyFields(errors,input); } if($(this).hasClass('match')){ errors = validateMatchingFields(errors,input,input_match); } if($(this).hasClass('email')){ errors = validateEmailAddress(errors,input); } }); //alert(errors); if(errors){ return false; } else {return true; } } function resetFormErrors(){ $('abbr,label,.item').removeClass('form-error'); $('span.form-error').remove(); $('input,select,textarea').removeClass('error-display'); //prevent multiple error displays on same element window.focused = false; //remove true } function errorMessage(input,message){ if(!input.hasClass('error-display')){ //prevent multiple error displays on same element input.addClass('error-display'); //prevent displaying error twice input.parent().addClass('form-error'); input.parent().children('label').children('abbr').addClass('form-error'); input.parent().children('label').append(' '+ message +''); } if(!window.focused){ input.focus(); window.focused = true; } //focused on first error return true; } function errorItemRemove(input){ $(input).parent().removeClass('form-error'); $(input).parent().children('label').children('abbr').removeClass('form-error'); $(input).parent().children('label').children('span.form-error').remove(); } function validateEmptyFields(errors,input){ if(input.val() =='' && !$(this).hasClass('email')){ errors = errorMessage($(input),'Please correct input'); //apply error message input.bind("keydown",function(){ errorItemRemove($(this)); }); //remove error message while being corrected } return errors; } function validateMatchingFields(errors,input,input_match){ if((input.val() != input_match.val()) && $(this).hasClass('match')){ errors = errorMessage($(input),'Inputs did not match'); //apply error message input.bind("keyup",function(){ //remove error message while being corrected if($(this).val() == $('[title='+$(this).attr('title')+']').not($(this)).val()){ errorItemRemove($(this)); errorItemRemove($('[title='+$(this).attr('title')+']').not($(this))); } }); } return errors; } function validateEmailAddress(errors,input){ var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i); if(!pattern.test( input.val() )){ errors = errorMessage($(input),'Please correct address format'); //apply error message input.bind("keydown",function(){ //remove error message while being corrected if(pattern.test( $(this).val() )){ errorItemRemove($(this)); } }); //remove error message while being corrected } return errors; } /*VALIDATE FORM*/