The min
rule set to 0
will ensure a field is greater than or equal to zero. You can also write a custom rule using .addMethod()
to ensure that it cannot be equal to zero.
$.validator.addMethod("greaterThan", function( value, element, param ) {
return this.optional( element ) || value > param;
}, "The value must be greater than {0}")
$('#myform').validate({
rules: {
myField: {
greatherThan: [0]
}
}
});
The {0}
does not represent the 0
character in the message. It represents a placeholder for the first parameter's value. So if the parameter is set to 0, the placeholder will automatically insert the parameter value into your custom message.
However, this plugin can not do validation of readonly
fields. When you think about it, it doesn't make any sense to validate something the user cannot directly control.
You, as the developer, are in control of the backend data programmatically, so make sure it's valid in the first place. In other words, if the sum of two fields must always be greater than zero, then you would write a custom method that looks at those two fields to make sure their sum is always greater than zero. Then when the user gets a validation error, it's only on the fields where the user can input their data.
Otherwise, you'll have to make some kind of workaround, where you programmatically copy the result into a hidden field and perform validation on this hidden input instead. Set the ignore
option to []
in order to ignore nothing. Otherwise, the plugin will ignore hidden input fields too.