[转]how can I change default errormessage for invalid price

时间:2023-01-24 17:11:35

本文转自:http://forums.asp.net/t/1598262.aspx?how+can+I+change+default+errormessage+for+invalid+price

I have:

[Required(ErrorMessage = "Price is required.")] public decimal price { get; set; }

How can I change default message for invalid price - I would like to change default error message "The value '45,8asasa' is not valid for Price.".

 

You cannot change this unforunately.
 See,
 http://forums.asp.net/p/1512140/3606268.aspx


 But using a javascript hack you can change the error message though,
 Just put this right after form closing tag,
 <script type="text/javascript">
             var vald = window.mvcClientValidationMetadata[0]['Fields'];
             for (var i = 0; i < vald.length; i++) {
                 for (var j = 0; j < vald[i].ValidationRules.length; j++) {
                     if (vald[i].ValidationRules[j].ErrorMessage.indexOf("must be a number.") > -1) {
                         vald[i].ValidationRules[j].ErrorMessage = vald[i].FieldName +" Error";
                     }
                 }
             }
         </script>

At server side you easily change it by playing with ModelState