The field must be a number

前端 未结 4 2127
死守一世寂寞
死守一世寂寞 2021-02-19 08:32

I have this field:

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

View:

 @Html.EditorFor(model => model.Pric         


        
相关标签:
4条回答
  • 2021-02-19 09:02

    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>
    
    0 讨论(0)
  • 2021-02-19 09:02

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

    [DisplayFormat(DataFormatString = "{0:N}", ApplyFormatInEditMode = true)]
    public decimal Price { get; set; }
    
    0 讨论(0)
  • 2021-02-19 09:02

    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.

    0 讨论(0)
  • 2021-02-19 09:09

    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

    0 讨论(0)
提交回复
热议问题