0

I want to validate one form in two part, when I click the btn button then validation is done properly, but when I click btnn it's not working and always validates field1

This is my code

jQuery:

$(document).ready(function() {
    $("#form1").validate({
        rules: {
            field1: "required"
        },
        messages: {
            field1: "Please specify your name"

        }
    })

    $('#btn').click(function() {
        $("#form1").valid();
    });
    $("#form1").validate({
        rules: {
            field2: "required"
        },
        messages: {
            field2: "Please specify your name"

        }
    })

    $('#btnn').click(function() {
        $("#form1").valid();
    });
});

HTML

<form id="form1" name="form1"> 
    Field 1: <input name="field1" type="text" />
     Field 2: <input name="field2" type="text" />
</form>

<div>
    <input id="btn" type="button" value="Validate"/>
    <input id="btnn" type="button" value="Validate"/>
</div>

Any suggestions?

Sparky
  • 98,165
  • 25
  • 199
  • 285
Mr. Tomar
  • 355
  • 3
  • 5
  • 22

1 Answers1

1

By design, you cannot call the .validate() method twice on the same form. The .validate() method is used for initialization and the second instance will always be ignored.

You have two options here:

  1. Put part 2 into its own <form> container.

OR

  1. Some other trick like showing/hiding the form fields, or using the .rules() method to add/remove rules dynamically depending on which button is clicked. If you're showing/hiding these fields, then declare all rules in the same instance of .validate() and leverage the default behavior of this plugin, which is to ignore all hidden fields.

Since you have not revealed the working concept of your multi-part form, or the purpose of wanting to validate in two parts, I will show you the simplest solution, which is suggestion #1.

jQuery:

$(document).ready(function() {

    $("#form1").validate({ // initialize `form1`
        rules: {
            field1: "required"
        },
        messages: {
            field1: "Please specify your name"
        }
    });

    $('#btn').click(function() {
        $("#form1").valid();
    });

    $("#form2").validate({ // initialize `form2`
        rules: {
            field2: "required"
        },
        messages: {
            field2: "Please specify your name"
        }
    });

    $('#btnn').click(function() {
        $("#form2").valid();
    });
});

HTML:

<form id="form1" name="form1"> 
    Field 1: <input name="field1" type="text" />
</form>

<form id="form2" name="form2">
    Field 2: <input name="field2" type="text" />
</form>

<div>
    <input id="btn" type="button" value="Validate"/>
    <input id="btnn" type="button" value="Validate"/>
</div>

Here is another answer showing how multi-step forms can be done:

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

Community
  • 1
  • 1
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • we cant do this with only one form ? – Mr. Tomar Jun 17 '16 at 05:42
  • @Tarzan, Yes, the showing/hiding trick works on one form. That's the point. Move all rules into the same instance of `.validate()` – Sparky Jun 18 '16 at 14:05
  • thanks sparky, i m using the second method, can we validate the hidden field according to steps i used the the ignore:[] but not work for me – Mr. Tomar Jun 23 '16 at 05:33
  • @Tarzan, since I cannot see your attempts at solving this, I would have absolutely no idea where you went wrong. *"not work for me"* is completely unhelpful. – Sparky Jun 23 '16 at 17:09
  • No worry :) Thanks for every thing. – Mr. Tomar Jun 24 '16 at 07:18