You must create a custom rule using the .addMethod()
method.
jQuery.validator.addMethod("dollarsscents", function(value, element) {
return this.optional(element) || /^\d{0,4}(\.\d{0,2})?$/i.test(value);
}, "You must include two decimal places");
DEMO: http://jsfiddle.net/w62Fb/
$(document).ready(function () {
jQuery.validator.addMethod("dollarsscents", function(value, element) {
return this.optional(element) || /^\d{0,4}(\.\d{0,2})?$/i.test(value);
}, "You must include two decimal places");
$('#payment-form').validate({ // initialize the plugin
rules: {
order_id: {
required: true
},
price: {
required: true,
number: true,
dollarsscents: true
}
},
// other options, etc.
});
});
Otherwise, to validate "dollars and cents", you might want to use the currency
method that's already part of the additional-methods.js
file. It validates two decimal places and you can toggle the symbol.
price: {
required: true,
currency: ['$', false] // false = dollar symbol not required
}
DEMO 2: jsfiddle.net/jz5xLeu1/