user1477388
user1477388

Reputation: 21440

Auto Tooltip Validation in MVC 4?

Where the heck are these things coming from? I like them, and I would like to leverage them elsewhere in my site. It appears they only show when I do regular expression validation in model:

[Display(Name = "Residential")]
[RegularExpression(@"[-+]?[0-9]*\.?[0-9]?[0-9]", ErrorMessage = "Must be a number")]
public Byte? residentialExperience { get; set; }

enter image description here

<div class="editor-label row">
    @Html.LabelFor(model => model.residentialExperience)
</div>
<div class="editor-field row">
    @Html.EditorFor(model => model.residentialExperience)
    @Html.ValidationMessageFor(model => model.residentialExperience)
</div>

How can I use these validation tooltips elsewhere? Also, how can I turn them off?

Also: It's not displaying the same message as I have in my model. It says, "Please enter a number" whereas I have written "Must be a number."

Upvotes: 3

Views: 2079

Answers (1)

Kenneth
Kenneth

Reputation: 28747

This is because you are outputting a numeric field. If you look at your HTML you will see that you have something like this:

<input type="number" ... />

By defining the type as a numbber, the browser knows what to expect and it will give you a generic message. This is part of Html 5 spec.

If you want to override the default behavior you could do this:

@Html.TextBoxFor(model => model.residentialExperience, new { @type = "text" })

Upvotes: 4

Related Questions