The field must be a number. How to change this message to another language?
Asked Answered
U

4

41

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?

Upset answered 23/8, 2012 at 20:27 Comment(3)
when you want show this message ?Rhodian
in really change the English message for spanish message will be the goalUpset
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)Upset
C
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://mcmap.net/q/263356/-providing-localized-error-messages-for-non-attributed-model-validation-in-asp-net-mvc-2

Crofton answered 22/8, 2013 at 23:37 Comment(10)
@ivowiblo: really... What's the problem you get? Maybe I can help you.Crofton
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.Canoewood
@ivowiblo I see... for old versions of ASP.NET MVC I think I used this solution: https://mcmap.net/q/263356/-providing-localized-error-messages-for-non-attributed-model-validation-in-asp-net-mvc-2 I'll update my answer to make this clear up front.Crofton
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.Algonkian
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 ?Herrmann
Field is not translated (by resource files or anything), but can be easily changed by adding [Display(Name = "YourText")].Anglin
But...Can I use other resource file from other library(from other assemble)? I don't want create the resource file on website project.Frustrate
@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: codeproject.com/Articles/778040/…Crofton
You can not use this approach for localizing PropertyValueRequired, see this SO thread for explanation.Fluorocarbon
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 ::?Stinnett
R
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; } 
Rhodian answered 23/8, 2012 at 20:47 Comment(3)
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?Upset
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 .Rhodian
and when you try to change decimal delimeter in jquery.validate.js then you will encounter another error.Lindley
N
1

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(
            _ => "Укажите численное значение!");
    });
Nightjar answered 18/10, 2022 at 5:0 Comment(0)
S
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 .

Shadow answered 24/7, 2014 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.