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?

1
  • possible duplicate? Commented Mar 31, 2011 at 14:15

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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, 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 with similar issue. But not an easy solution. I'm all out of ideas...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.