asp.net mvc validation must be a number custom error

前端 未结 10 1151
时光取名叫无心
时光取名叫无心 2021-02-06 01:14

I am new to asp.net and I have a problem. When the users insert in a editor for a decimal field something other than numbers, they get an error \"Field name\" is not a number. B

相关标签:
10条回答
  • 2021-02-06 01:51

    You could implement your own custom validation attribute: http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx

    0 讨论(0)
  • 2021-02-06 01:51

    It seems that since Para's answer MVC evolved and now the ClientDataTypeModelValidatorProvider accepts a ResourceClassKey property. It uses the FieldMustBeNumeric and FieldMustBeNumeric messages specified in your resource class.

    0 讨论(0)
  • 2021-02-06 01:54

    To change the error message you get after server side validation you need to change 'PropertyValueInvalid' key in your resource file and assign the resource file name to DefaultModelBinder.ResourceClassKey. See this question for details: localize default model validation in mvc 2

    0 讨论(0)
  • 2021-02-06 01:56

    A quick and simple hack for Customize RangeValidator ErrorMessage --"'Field name' is not a number"-- is using RegularExpression

    [Range(0.5, 1000, ErrorMessage = "Amount should be in range {1} to {2}.")]
    [DataType(DataType.Currency)]
    [RegularExpression(@"\d", ErrorMessage = "Amount is not valid.")]
    public decimal Amount{ get; set; }
    
    0 讨论(0)
提交回复
热议问题