/*indoc form validation - add on load??*/ $('document').ready(function(){ //get all form elements which need to be validated $('.duoformer').hide(); $('.formrow input').bind('blur',validateFormElementEvent); $('.formrow select').bind('blur',validateFormElementEvent); $('.formrow textarea').bind('blur',validateFormElementEvent); $('.formrow .submit').bind('click',validateAllForm); duoslideshow_default(); function validateFormElementEvent(event){ if($(event.target).attr('type')!="submit"){ validateFormElement(event.target,false); } } function validateFormElement(target,instant){ //first remove all current errors var fieldName=$(target).attr('name'); var error; if(fieldName){ //get validation type var validationType=fieldName.replace(/.+__/,''); var fieldValue=$(target).val().replace(/^\s+|\s+$/g, '');//trim if(validationType=="mandatory" && $(target).attr('type')=="checkbox" && !$(target).attr('checked')){ error="You must tick this"; } if(validationType=="mandatory" && $(target).val()==""){ if($(target)[0].tagName=="SELECT" ){ error="You must select an option"; }else if($(target).attr('type')!="checkbox"){ error="You must fill out this field"; } } if(validationType=="email" && !fieldValue.match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/)){ error="This must contain an email address"; } if(validationType=="phone" && !fieldValue.match(/[0-9 .()+-]{6,20}/)){ error="This must contain a telephone number"; } if(validationType=="url" && !fieldValue.match(/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/)){ error="This must contain a website address"; } if(validationType=="age" && (fieldValue-0)>0 && (fieldValue-0)<160 ){ error="This must contain an age"; } if(validationType=="number" && (fieldValue.match(/[^0-9\., ]+/) || fieldValue.length<1)){ error="This must contain a number "; } if(validationType=="ukpostcode" && (!fieldValue.match(/[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}/i) || fieldValue.length<1)){ error="This must contain a valid uk postcode "; } } if(error){ //absolutely postion an error near field?, then set a timer to remove them all after 5 seconds? if($(target).parent().find('.formError').length==0){ $(target).after('
'+error+'
'); $(target).next().fadeIn(1000); } }else{ //add tick? $(target).parent().addClass('valid'); if(instant){ $(target).parent().find('.formError').remove(); }else{ $(target).parent().find('.formError').fadeOut(1000,function(){ $(this).remove(); }); } } } function validateAllForm(event){ $(event.target).parents('form').find('input').each(function(index){ if($(this).attr('type')!="submit" && $(this).attr('type')!="hidden" && !$(this).hasClass('duoformer')){ validateFormElement($(this),true); } }); $(event.target).parents('form').find('textarea').each(function(index){ validateFormElement($(this),true); }); $(event.target).parents('form').find('select').each(function(index){ validateFormElement($(this),true); }); if($(event.target).parents('form').find('.formError').length>0){ alert('Please correct the errors in the form before sending'); event.preventDefault(); return false; } } }); function duoslideshow_default(){ $(".duoslideshow_default").each(function(){ var slideid="#"+$(this).attr('id'); var pagerid="#pager_"+(slideid.replace(/[^0-9]+/i,'')); $(pagerid).html(''); slideid=slideid+" .slides"; $(slideid).cycle({ fx: 'fade', pager:pagerid, speed:2000, pagerAnchorBuilder: function(idx,slide){ return ''+(idx+1)+''; } }); }); }