0

I'm working on a mvc5 app and I need my validation messsages to be in spanish and i'm using data annotations to achieve this, the problem is that only work for the Required annotation, here is the code i'm using for that.

Model

[Required(ErrorMessage = "El numero de empleado no ha sido ingresado")]
    [Display(Name = "Total de empleados de la empresa")]
    [RegularExpression("^[0-9]+$", ErrorMessage = "Debe ser un número.")]
    public int NumeroEmpleados { get; set; }

View

@Html.EditorFor(model => model.NumeroEmpleados, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NumeroEmpleados, "", new { @class = "text-danger" })

the problem is that is showing the default message, which is in english, like this.

"The field NumeroEmpleados must be a number. "

EDIT

So, I solved it using jquery.validation localization features, now my number fields are correct, but my currency fields are not, currently they are something like this.

[Required]
    [Display(Name = "Recaudo CE")]
    [RegularExpression("^[0-9]+$", ErrorMessage = "Ingrese un valor apropiado")]
    [DataType(DataType.Currency)]
    [DisplayFormat(DataFormatString = "{0:C0}")]
    public decimal RecaudoCE { get; set; }
A Alvarez
  • 1
  • 2

1 Answers1

0

The default message is not coming from the RegularExpressionAttribute, it's from trying to bind a non-integer value to an integer property.

You could make NumeroEmpleados a String, and do the conversion to integer in your controller.

See also: How to change the default "The field must be a number"

Community
  • 1
  • 1
Mike G
  • 133
  • 1
  • 7