Imagine you have two fields in your Model:
public class MyModel
{
[Required(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "DateRequired"]
public DateTime Date;
[DataType(DataType.Currency, ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "NumberError")]
public decimal Number;
}
My problem is this: If the user enters a non-valid Date (like ‘aaa’) or a non-valid Number (like ‘bbb’) then the standard jQuery validation messages kicks in like: ‘The field Number must be a number’ (and the same goes for Date).
How do I get a localized error message for my fields? I know I can convert my fields to strings and then manually convert these fields to my corresponding database fields, but I believe there must be an easier way.
You should load the correct “messages_##.js” file for your language. For instance, when I need brazilian portuguese validation, I use:
http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/localization/messages_ptbr.js
Just replace “ptbr” at the end with your localization code. Of course, you can just get the code and use your own messages.