December 13 2016

Jquery validation validate money method function

Tagged Under :

javascript
Jquery validation itself didn’t give money validate method function. What we can to do is write a custom method to validate the money format with regular expression.

Below is the money validate method.
jQuery.validator.addMethod(
    "money",
    function(value, element) {
        var isValidMoney = /^\d{0,4}(\.\d{0,2})?$/.test(value);
        return this.optional(element) || isValidMoney;
    },
    "Insert "
);

Below is the example how to use the money validation method
$("#myForm").validate({
rules: {
    numberInputField: {
        money: true,
    }
}
});

Make a Comment

You must be logged in to post a comment.