1

I am working on this wizard that have 5 steps but a single form, the intent is to process the data on a single server request when the user hits the Finish button.

I am trying to use Jquery Validate plugin to validate each step separately, my original idea was to reinitialize the plugin every time the user clicks next, like that:

function validateStepOne(){

    $("#form").validate({
        rules: {
            x: "required"
     }}); 

    if(!$("#form").valid()){
        return false;
    }

    return true;
}

function validateStepTwo(){
    $("#form").validate({
        rules: {
        y : "required",
    }}); 

    if(!$("#form").valid()){
       return false;
    }

    return true;
}

But that doesnt seems to work because on second+ steps the forms is always marked as valid, so I am wondering how can I reset the validation plugin to be able to validate the same form more than once.. Any help is welcome

Sparky
  • 98,165
  • 25
  • 199
  • 285
Marcos J.C Kichel
  • 6,887
  • 8
  • 38
  • 78
  • Try to remove the validation with: $('#formid').removeData('validator'); between the validations. Or can you have multiple forms in your page with unique validation? – Arg0n Nov 20 '15 at 20:44
  • 1
    You can **not** reinitialize the plugin. It can only be initialized once on the form. Your only options are to break it up into five different `form` containers OR use the `.rules()` method to activate/deactivate rules as needed. Otherwise, the plugin will ignore hidden fields by default, and for some people, this is good enough for this scenario. – Sparky Nov 20 '15 at 20:48
  • See this answer: http://stackoverflow.com/a/19546698/594235 – Sparky Nov 20 '15 at 20:58
  • @Sparky I ended up figuring out that the hidden fields are ignored, so I just inserted all the rules on a single validation method, as you suggested above, anyway if you like to create an answer I can for sure accept it. – Marcos J.C Kichel Nov 20 '15 at 22:55

1 Answers1

1

You can not reinitialize the plugin. It can only be initialized once on the form. Your options are to break it up into five different form containers OR use the .rules() method to activate/deactivate rules as needed.

Otherwise, hidden fields are ignored by default. So typically on a multi-step form, when the fields in the non-active tabs are hidden, they are no longer validated. This is the approach many people will take depending on the type of multi-step form.

Sparky
  • 98,165
  • 25
  • 199
  • 285