puks1978
puks1978

Reputation: 3697

jquery validate

I am using the plugin from here: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

and I am wondering if anyone has any code that will allow me to compare 2 form fields and validate that field_1 must be larger than field_2?

I am initiated the validate method like:

$("#frmValidate").validate({
    meta: "validate"
});

Many thanks.

Upvotes: 0

Views: 505

Answers (2)

Pawel Markowski
Pawel Markowski

Reputation: 1196

You need to declare the validation method, something like that:

$.validator.addMethod(
    "greaterThan",
    function(value,element,params) {
        if (value > $(params).val()) {
            return true;
        }
        return false;
    },
    "Wrong"
);

And then use it in validation.

$('#my_form').validate({
        rules: {
            'field1': {
                required: true,
                greaterThan: '#field2'
            }
        }
});

Upvotes: 2

Adrian Grigore
Adrian Grigore

Reputation: 33318

This can be done in a simple two-step process:

  1. Declare your own validation method. In that method you would compare the two input elements.
  2. Reference the validation method in one of the two input elements.

Upvotes: 0

Related Questions