performance - jQuery validating multiple form fields -


fairly new jquery realize code condensed. works fine there's got better way kind of seems redundant code. i've got 2 input fields , textarea i'm validating see below. code samples ideal since i'm still learning appreiacted:

$("form#designform").submit(function(){      if($("#ccdesignfirstname").val().length == 0){       alert("full name field required");       $("#ccdesignfirstname").focus();       return false;     }              if($(".telephoneinput").is(":visible")){         if($("#ccdesigntelephone").val().length == 0){           alert("phone required");           $("#ccdesigntelephone").focus();           return false;         }     }      if($("#ccdesignproblem").val().length == 0){       alert("question required");       $("#ccdesignproblem").focus();       return false;     } }); 

to iterate through inputs in form can this:

$("form#designform").submit(function(){     var valid = true;     $("form#designform :input").each(function(){         //$(this) jquery object of input.         //do validation here         if($(this).val().length == 0){             valid = false;         }         //some more validation if needed...     });     return valid; }); 

this uses jquery [:input selector][1] types of inputs, if want text can :

$("form#designform input[type=text]") 

etc.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -