/*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,64})$/)){ 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('