0

I need to validate that a select box value is greater than zero, only if another condition exists. I have a range of things in place, and from what I can tell it should work, but it's not.

My Custom Validation Method:

jQuery.validator.addMethod("greaterThanZero", function(value, element) {
    return this.optional(element) || (parseFloat(value) > 0);
}, "* Amount must be greater than zero");

This works fine (in that it validates that address_stateid is greater than zero): address_stateid: { "greaterThanZero":true },

This also works fine (in that it DOES NOT validate that address_stateid is greater than zero): address_stateid: { "greaterThanZero":false },

However, when I change true/false to be a conditional, such as this, it just behaves as if it is true, regardless of the value of require_state.

address_stateid: { "greaterThanZero":function(){return require_state;} },//ALWAYS VALIDATES AS IF require_state IS TRUE

the require_state value is set to true or false based on selection of another field. When this changes, I am outputting the value of 'require_state' to the console,and can see that even when it is set to false, the conditional validation is kicking in, but it shouldn't.

Can anyone see what I am doing wrong here?

Thanks

4

1 回答 1

0

检查这个小提琴,我认为这将帮助你实现你的要求

var require_state = true;
jQuery.validator.addMethod("greaterThanZero", function(value, element) {
 return this.optional(element) || (parseFloat(value) > 0 ||   require_state);

}, "* 金额必须大于零");

http://jsfiddle.net/Shinov/DfyqE/1/

于 2013-06-26T06:57:11.463 回答