Only Bolivian Here
Only Bolivian Here

Reputation: 36743

MVC3 error message declared in my model isn't taking effect

[Required(ErrorMessage = "ONLY TYPE IN NUMBERS!")]
[Display(Name = "Telefono Fijo")]        
public int Telephone { get; set; }

Basically, I'd like that when someone types in a letter, that text up there should display.

Here's my view:

<div>
    @Html.LabelFor(model => model.RegisterModel.Telephone)
    @Html.EditorFor(model => model.RegisterModel.Telephone)
    @Html.ValidationMessageFor(model => model.RegisterModel.Telephone)
</div>

When I type in letters, I get:

"The field Telefono Fijo must be a number."

And when I don't type in ANYTHING, I get:

"ONLY TYPE IN NUMBERS!"

Any ideas? I only want the custom message to show.

Upvotes: 0

Views: 1503

Answers (1)

vladimir
vladimir

Reputation: 15218

You should use RegularExpressionAttribute:

[RegularExpression(@"^\d+$", ErrorMessage = "ONLY TYPE IN NUMBERS!")]

Upvotes: 2

Related Questions