.NET

MVC 3 problems validating a decimal

Last I was doing some MVC3 development but I kept on having validation problems with my forms.
My form looked like this:

image

I don’t know if you see my problem already but I had some problems validating my decimal.
When I submitted my form I always get the following error message: The field Max. aantal uren per VTE must be a number.
After a while I discovered what the problem was. I live in Belgium and here we write our numbers like 1000,1 . By default MVC3 only can parse this number written like 1,000.1 (other culture than nl_BE).
The nl_BE culture is not (yet) implemented in MVC3 but I found a workaround for this problem.

You can extend the jQuery validation with an extra script.
I used following javascript method to solve my validation error.

jQuery.extend(jQuery.validator.methods, {
    date: function (value, element) {
        return this.optional(element) || /^\d\d?\.\d\d?\.\d\d\d?\d?$/.test(value);
    },
    number: function (value, element) {
        return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value);
    }
});

In this code you can see that I changed the validation method for date and number. In this case it is the number function that solved my problem.

Hopefully I can save the people who are writing applications for this type of culture some time searching out the scripts.

Leave a Reply

Your email address will not be published. Required fields are marked *