Server side validation works fine, but the client side will not work in some fields.
Using:
"jquery-1.5.1.min.js"
"modernizr-1.7.min.js"
"jquery.validate.min.js"
"jquery.validate.unobtrusive.min.js".
HTML5 Text Markup
MVC3, Razor, Code First
Class data annotations:
using System.ComponentModel.DataAnnotations;
[Required, MaxLength(30, ErrorMessage = "First Name must be {1} characters or less")]
public string FirstName { get; set; }
[Range(20, 50, ErrorMessage = "{0} must be between {1} and {2}")]
public double Waist { get; set; }
View:
The view is strongly typed.
@using (Html.BeginForm()) { @Html.ValidationSummary(true)
<span class="editor-label"> First Name </span>
<span class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</span><br /><br />
<span class="editor-label">@Html.LabelFor(model => model.Waist): 20 to 50</span>
<span class="editor-field">
@Html.EditorFor(model => model.Waist)
@Html.ValidationMessageFor(model => model.Waist)
</span><br /><br />
Client-side validation works for the waist, but not for FirstName. The server side works for both.
When I look at the source code, the waist has a data-val range.
data-val-range = "Waist should be between 20 and 50"
FirstName does not have a data-val range.
Any help is greatly appreciated.
Thank,
Joe
I am using the 64-bit version of IE9.
With further research, Required and String Length partially work. I changed the Dataannotation attributes to this.
[Required(ErrorMessage = "First Name is required")] [StringLength(30, MinimumLength = 2, ErrorMessage = "First Name must be {2} to {1} characters")] public string FirstName { get; set; }
, FirstName , . , StringLength, , Required. , .
data-val-length = , StringLength