jquery validate dependency callback on "minvalue"?
Is it possible to write a dependency开发者_如何学JAVA callback for the minValue rule, such as you can with the required text?
I have 2 required fields -> Price Paid and Price Seen (for a rebate form). I tried to write something like this:
minValue: function(element) {
$("#PP_PriceSeen").val();
}
if I try to set an alert or something, I do get the correct value, but it isnt being passed into the rule. Is this only allowed for the 'required' option?
Thanks
It would be easy enough to accomplish this with a custom validator rule:
$.validator.addMethod("minDependency", function (value, element, params) {
return this.optional(element) || ((parseInt(value, 10) || 0) >= (parseInt($(params).val(), 10) || 0));
}, "Please enter at least the minimum value");
$("#test").validate({
rules: {
fieldName: {
minDependency: "#min"
}
}
});
This defines a new rule minDependency
that accepts a selector as a parameter. The selector's value is compared against the element under validation.
Example: http://jsfiddle.net/andrewwhitaker/wqSGE/
The val()
method is jQuery's default method. It returns value of element.
You might wanted to use validate()
method of jQuery validation plugin. If you want to validate "minvalue" you can do it like this:
$(document).ready(function(){
$("#myform").validate({
rules: {
field: {
required: true,
minlength: 3
}
}
});
});
You can find full code of the example in here. You can also find other informations in this documentation.
精彩评论