40

How can I change that messages for all int fields so that instead of saying:

The field must be a number in English, it shows:

El campo tiene que ser numerico in Spanish.

Is there are a way?

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Diego_DX
  • 1,051
  • 1
  • 20
  • 33
  • when you want show this message ? – M.Azad Aug 23 '12 at 20:34
  • in really change the English message for spanish message will be the goal – Diego_DX Aug 23 '12 at 20:38
  • 1
    sorry i want to show that message when in the texbox thas is model.int somebody write string on it, with jquery.validate and jquery.validate.unobtrusive show that message in english (I need to that message is in spanish for all my int field) – Diego_DX Aug 23 '12 at 20:40

4 Answers4

66

If you happen to be using ASP.NET MVC 4 onwards, check this post:

Localizing Default Error Messages in ASP.NET MVC and WebForms

Basically you have to add the following piece of code in your Application_Start() method in Global.asax:

 ClientDataTypeModelValidatorProvider.ResourceClassKey = "Messages";
 DefaultModelBinder.ResourceClassKey = "Messages";

Right click your ASP.NET MVC project in Solution Explorer inside Visual Studio and select Add => Add ASP.NET Folder => App_GlobalResources.

Now add a .resx file inside this folder called Messages.resx.

Finally add the following string resources in that .resx file:

Name                   Value
====                   =====
FieldMustBeDate        The field {0} must be a date.
FieldMustBeNumeric     The field {0} must be a number.
PropertyValueInvalid   The value '{0}' is not valid for {1}.
PropertyValueRequired  A value is required.

You should be good to go.

Note that the value you're interested in is the FieldMustBeNumeric. To localize it to Spanish, you have to add another resource file named Messages.es.resx. In this specific .resx file replace the resource value with:

Name                Value
====                =====
FieldMustBeNumeric  El campo {0} tiene que ser numerico.

If you happen to be using ASP.NET MVC 3 downwards, this solution can help you achieve the same result: https://stackoverflow.com/a/2551481/114029

Community
  • 1
  • 1
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • @ivowiblo: really... What's the problem you get? Maybe I can help you. – Leniel Maccaferri Aug 24 '13 at 18:52
  • ClientDataTypeModelValidatorProvider doesn't have a ResourceClassKey property. That's the problem in MVC3, it has the texts in a hardcoded resource file and they fixed it in MVC4. – Ivo Aug 24 '13 at 18:55
  • @ivowiblo I see... for old versions of ASP.NET MVC I think I used this solution: http://stackoverflow.com/a/2551481/114029 I'll update my answer to make this clear up front. – Leniel Maccaferri Aug 24 '13 at 18:58
  • 2
    After a long search this answer helped me. I was confused between Javascript validation and the *unobstrusive* validation, which uses the `data-val-number` HTML5 attribute. Thank you. – Leonardo Herrera Oct 11 '13 at 18:36
  • I am getting the word in {0} in English and the rest is coming up from the translation. How do we translate the Field as well ? – Nick Sep 30 '14 at 15:52
  • 1
    Field is not translated (by resource files or anything), but can be easily changed by adding [Display(Name = "YourText")]. – ilter Oct 05 '14 at 16:40
  • 1
    But...Can I use other resource file from other library(from other assemble)? I don't want create the resource file on website project. – qakmak Oct 10 '14 at 04:50
  • @qakmak: Sure, you can. Just create a separate class library and add your `.resx` files there. Then reference this library in your web project. That's it. For more info, check here: http://www.codeproject.com/Articles/778040/Beginners-Tutorial-on-Globalization-and-Localizati – Leniel Maccaferri Oct 10 '14 at 13:40
  • You can not use this approach for localizing `PropertyValueRequired`, see [this SO thread](http://stackoverflow.com/a/12545997/3242721) for explanation. – Michal Hosala Oct 19 '15 at 20:07
  • Thank you so much. Do you know how to eliminate the :: after the field name? If I put The filed {0} must be a num.. it will say for The field Number:: must be a num. How to remove those ::? – Alexander Apr 05 '18 at 13:35
7

you can set your custom message for your validation.

 [RegularExpression("\d{9}",ErrorMessage="El campo tiene que ser numerico")]
 public decimal UnitPrice { get; set; } 
M.Azad
  • 3,673
  • 8
  • 47
  • 77
  • 1
    yep I know that way but there is not a way that i can set that for all my int insteed of doing field for field? – Diego_DX Aug 23 '12 at 20:52
  • Yeah.I think that you do it..or create a public mask for your control that bind to a numeric type property.Or define your property in base class if your property is a common property . – M.Azad Aug 23 '12 at 21:01
  • and when you try to change decimal delimeter in jquery.validate.js then you will encounter another error. – ohdev Jul 05 '20 at 18:48
0

If you want to specify custom message for each Integer , double and float . you can use Range Attribute with String as below.

    [Required(ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "YearOfEstablishmentRequired")]
    [Range(0, int.MaxValue, ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "ValidYearOfEstablishment")]
    [Display(Name = "Year Of Establishment")]
    public string YearOfEstablishment { get; set; }

Now as above you can specify custom message for each and every propery .

Dilip Langhanoja
  • 4,455
  • 4
  • 28
  • 37
0

For those using Razor Pages, this code may help (should be placed in Program.cs or Startup.cs)

builder.Services.AddRazorPages().AddMvcOptions(options =>
    {
        options.ModelBindingMessageProvider.SetValueMustBeANumberAccessor(
            _ => "Укажите численное значение!");
    });
Vizel Leonid
  • 456
  • 7
  • 15