views:

292

answers:

2

Hi there

I'm trying to do some validation on 2 numerical fields and not done much JQuery validation this year.

-Product A comes with Product B. -You can have unlimited amount of Product A -But Product B can only have the same or less than Product A

For example: If Product A qty is 5 then product B can be 1 to 5.

I have tried an validation.addmethod but ended up all confussed. The product ids for A and B are autogenerated but i it is possible to matach Product B to A.

Any ideas?

A: 

I am assuming that you are using this validation plugin.

First suggestion is to put valiadation actions on change event. On change A validate B. There is max rule.

On submit revalidate.

Elzo Valugi
+1  A: 

In document ready function, add custom validation check_b method as a rule for Product B ( ( Id of input of the product B is prod_b and Id of the input of product A is prod_a).

$(form_name).validate(({
          rules: {
           "prod_b": {
                check_b: true
            }
          }
         }));

$.validator.addMethod("check_b", function( value, element, param ) {

        var val_a = $("#prod_a").val();

        return this.optional(element)
            || (value <= val_a);
    },"Your error message.");

In the custom method, you get the value of Product A and check whether value of Product B is less than that.

Manjula