How can I do a conditional success check with the JQuery Validation Plugin?

后端 未结 2 587
滥情空心
滥情空心 2021-01-26 06:09

I am attempting to use the JQuery Validation Plugin but am running in to a small problem. My form uses the following method to hide/show the desired content:

&         


        
相关标签:
2条回答
  • 2021-01-26 06:35

    The thing you need to do is not let them change pages until the form is valid. The default in jQuery Validate is that only "visible" fields are validated, which will work to your advantage.

    You're on the right track with doing a validation before showing the next "layer".

    First off, see the example from the documentation.

    So the equivalent for you would be to do this:

      //remember to save the validation object like this
      var v = $(".selector").validate({ /* ... */});  
    
      //then add a click handler for your button
      $("#C1").click(function() {
          //this runs the validation and shows errors if it fails
          if (v.valid()) {
              //only move forward if the validation succeeds
              showLayer('page2');
          }
      });
    
    0 讨论(0)
  • 2021-01-26 06:41

    There are lots of ways to do this, but the demo below also contains a large part of the answer to your question, which would be to use the .valid() method to programatically test the form. You can use my example below in whole or in part.


    When I create multi-step forms, I use a unique set of <form> tags for each section. Then I use the .valid() method to test the section before moving to the next. (Don't forget to first initialize the plugin; call .validate(), on all forms on DOM ready.)

    Then on the last section, I use .serialize() on each form and concatenate them into a data query string to be submitted.

    Something like this...

    $(document).ready(function() {  // <-- DOM ready handler
    
        $('#form1').validate({ // initialize form 1
            // rules
        });
    
        $('#gotoStep2').on('click', function() { // go to step 2
            if ($('#form1').valid()) {
                // code to reveal step 2 and hide step 1
            }
        });
    
        $('#form2').validate({ // initialize form 2
            // rules
        });
    
        $('#gotoStep3').on('click', function() { // go to step 3
            if ($('#form2').valid()) {
                // code to reveal step 3 and hide step 2
            }
        });
    
        $('#form3').validate({ initialize form 3
            // rules,
            submitHandler: function (form) {
               // serialize and join data for all forms
               var data = $('#form1').serialize() + '&' + $('#form2').serialize() + '&' + $(form).serialize()
               // ajax submit
               return false; // block regular form submit action
            }
        });
    
        // there is no third click handler since the plugin takes care of this 
        // with the built-in submitHandler callback function on the last form.
    
    });
    

    Important to remember that my click handlers above are not using type="submit" buttons. These are regular buttons, either outside of the form tags or type="button".

    Only the button on the very last form is a regular type="submit" button. That is because I am leveraging the plugin's built-in submitHandler callback function on only the very last form.

    "Proof of Concept" DEMO: http://jsfiddle.net/dNPFX/

    Also, see for reference:

    https://stackoverflow.com/a/19546698/594235

    0 讨论(0)
提交回复
热议问题