$(".validateform").bind('submit',function (e){ var toreturn = true; //email //required $(this).find(".required").each(function (e){ if(!requiredfield($(this).attr('value'))) { toreturn=false; $(this).addClass('error'); $(this).bind('keyup',function(e){ if(requiredfield($(this).attr('value'))) { $(this).removeClass('error'); $(this).unbind('keyup'); errorfree(); } }); } //radio else if($(this).attr('type')=='radio') { if($(this).parent().find('input[checked=true]:radio').size()==0) { toreturn=false; $(this).parent().addClass('error'); $(this).parent().click(function(e){ if($(this).find('input[checked=true]:radio').size()>0) { $(this).removeClass('error'); $(this).unbind('click'); errorfree(); } }); } } //checkbox else if($(this).attr('type')=='checkbox') { if($(this).parent().find('input[checked=true]:checkbox').size()==0) { toreturn=false; $(this).parent().addClass('error'); $(this).parent().click(function(e){ if($(this).find('input[checked=true]:checkbox').size()>0) { $(this).removeClass('error'); $(this).unbind('click'); errorfree(); } }); } } else { } }); //email $(this).find(".email").each(function (e){ if(!emailfield($(this).attr('value'))) { toreturn=false; $(this).addClass('error'); $(this).bind('keyup',function(e){ if(emailfield($(this).attr('value'))) { $(this).removeClass('error'); $(this).unbind('keyup'); errorfree(); } }); } }); //integer $(this).find(".int").each(function (e){ if(!intfield($(this).attr('value'))) { toreturn=false; $(this).addClass('error'); $(this).bind('keyup',function(e){ if(intfield($(this).attr('value'))) { $(this).removeClass('error'); $(this).unbind('keyup'); errorfree(); } }); } }); if(!toreturn) { $(".errormessage").css('display','block'); } return toreturn; }); function errorfree() { if($(".error").size()==0) $(".errormessage").css('display','none'); } function requiredfield(value) { if(value=="") return false; return true; } function intfield(value) { return (value.toString().search(/^[0-9]+$/) == 0); } function emailfield(value) { var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; if(reg.test(value) == false) return false; return true; }