18

I have this field:

public decimal Price { get; set; } in Database it is decimal (7,2).

View:

 @Html.EditorFor(model => model.Price, 
                 new { htmlAttributes = new { @class = "form-control" } })

If i put a value with comma, MVC default validation doesn't accept, says: "The field must be a number". (I tried use a Regex, but no way)

For example: 5,00, 55,00 or 555,00

Also this:

public DateTime date { get;set; }

View:

 @Html.EditorFor(model => model.Date, 
                 new { htmlAttributes = new { @class = "form-control" } })

MVC default validation doesn't accept dates in format dd/mm/yyyy, only in mm/dd/yyyy.

For example: 13/02/2015, 15/06/2013, 25/08/2012

Is something with globalization or what? How can I solve this?

Sachu
  • 7,555
  • 7
  • 55
  • 94
developer033
  • 24,267
  • 8
  • 82
  • 108
  • There are two different question, it's better create separate question. For datetime format you can use globalization setting and update the thread to any culture that you want. For first question you can use Money data type instead of int – Peyman May 26 '15 at 03:37
  • Are you assigning from a string? Can we see the code where you assign the value? – Paul Sasik May 26 '15 at 03:37
  • This is indeed a globalization problem. Are you experiencing problems with client-side validation, server-side, or both? – lc. May 26 '15 at 03:38
  • I edited my question, I just create a project MVC with EF and generate the controllers + Views and when I digit some value with comma it happens or a date in format dd/mm/yyyy. // With client-side. – developer033 May 26 '15 at 03:46
  • Is there a solution for it? – developer033 May 26 '15 at 19:44
  • @developer033 : have you found the solution? – Jun Rikson Aug 25 '18 at 16:04

4 Answers4

24

One solution I found was to override the validation functions of jquery.validate.js


<script>

    $.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);
    }
    //Date dd/MM/yyyy
    $.validator.methods.date = function (value, element) {
        var date = value.split("/");
        return this.optional(element) || !/Invalid|NaN/.test(new Date(date[2], date[1], date[0]).toString());
    }
</script>
Tiago S
  • 1,299
  • 23
  • 23
1

You may want to decorate your field with the [DisplayFormat] attribute as bellow:

[DisplayFormat(DataFormatString = "{0:N}", ApplyFormatInEditMode = true)]
public decimal Price { get; set; }
Tran Nguyen
  • 1,341
  • 10
  • 15
1

You might find your answer here error with decimal in mvc3 - the value is not valid for field , it didn't work for me so i used this temporary

<div class="col-md-10">
            @{ Html.EnableClientValidation(false); }
            @Html.EditorFor(model => model.DecimalValue, new { htmlAttributes = new { @class = "form-control" } })
            @{ Html.EnableClientValidation(true); }
            @Html.ValidationMessageFor(model => model.DecimalValue, "", new { @class = "text-danger" })
        </div>

and i find this here ASP .NET MVC Disable Client Side Validation at Per-Field Level

Community
  • 1
  • 1
JohanH
  • 19
  • 2
0

I have the same issue, I used to solve it with the globalisation library (globalize.js) but they changed it so it doesn't include the localisation files. It is supposed to get them from the cldr library, but I haven't figured out how.

LuckyNL
  • 1
  • 4