5

I'm stuck with ASP.NET MVC 3 jQuery unobtrusive validation message localisation. Specifically with "number" validation. If I have a number property in model input html is rendered with data-val-number attribute with value "The field Quantity must be a number." How I can localize this string. With data annotation attributes there is no problem to define localized message. But for number validation I do not have to specify any attribute.

So, how can be localized validation messages generated by unobtrusive validation?

Atilla Ozgur
  • 14,339
  • 3
  • 49
  • 69
Kazys
  • 377
  • 2
  • 12
  • possible [duplicate?](http://stackoverflow.com/questions/4333125/localizing-validation-messages-at-client-side-by-jquery-in-asp-net-mvc) – mateuscb Mar 31 '11 at 14:15

2 Answers2

3

I got to solution refering this article http://jwwishart.wordpress.com/2010/03/22/custom-server-and-client-side-required-validator-in-mvc-2-using-jquery-validate/

It works, but still very unconvenient.

If there is only one culture it could be convenient to use

$('input[data-val-number]').attr('data-val-number', 'Custom message');

This script must go before

<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
JasCav
  • 34,458
  • 20
  • 113
  • 170
Kazys
  • 377
  • 2
  • 12
0

Found something. This blog explains step by step how to accomplish this. I just tried a quick run through using MVC 3 unobstructed validation and it worked perfect.

Basically, you add a resource, and use validation attribute like this:

[Range(1, 130, ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "Range")]
public string myNumber { get; set; }

The result was client validation with my custom string.

mateuscb
  • 10,150
  • 3
  • 52
  • 76
  • It is ok when you use attributes. In case above attribute is not specified and validation is added by property type, so there is no way to define resources. Your defined way solves localization the problem, but it still a little bit awkward workaround becouse on every numeric property I have to define Range attribute, think of possible ranges – Kazys Apr 01 '11 at 07:30
  • @Kazys, I understand the problem a better now. And you are right, having to add the range to all numeric fields is quite awkward. I found another [post](http://stackoverflow.com/questions/4828297/how-to-change-data-val-number-message-validation-in-mvc-while-it-generate-by-he) with similar issue. But not an easy solution. I'm all out of ideas... – mateuscb Apr 02 '11 at 16:08