by Stian

Decimal validation with comma in MVC

20130228DecimalSeparator.svgIn MVC 3, when using jQuery validation plugin with unobtrusive validation, you will see that comma in a textbox that is bound to a decimal value will not be allowed by client side validation.

Instead of going in and changing the jquery.validate.js, you should override the default behavior by adding these lines to your own javascript file (make sure it is loaded after the valiate javascript!)

$.validator.methods.range = function(value, element, param) {
    var globalizedValue = value.replace(",", ".");
    return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
};

$.validator.methods.number = function(value, element) {
    return this.optional(element) || /^-?(?:d+|d{1,3}(?:[s.,]d{3})+)(?:[.,]d+)?$/.test(value);
};

This will fix your problem for the number validation and the range validation!

  • Anonymous

    Hello!

    Thanks for the post, I try to use it today with Jquery val 1.11 and it doesn’t seem to work.

    Do you know what do I have to change in orther to make it work?