Client side MVC3 check is intermittent / inconsistent

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

+5
1

MaxLength MinLength . StringLength:

[Required]
[StringLength(30, ErrorMessage = "First Name must be {1} characters or less")]
public string FirstName { get; set; }

MinimumLength, , MinLength:

[Required]
[StringLength(30, MinimumLength = 10, ErrorMessage = "First Name must be between 10 and 30 characters")]
public string FirstName { get; set; }

UPDATE:

, , . script, :

<script type="text/javascript">
    $(function () {
        var settngs = $.data($('form')[0], 'validator').settings;
        settngs.onfocusout = function (element) { $(element).valid(); };
    });
</script>

2:

. ASP.NET MVC 3 -.

:

public class MyViewModel
{
    [Required(ErrorMessage = "First Name is required")]
    [StringLength(30, MinimumLength = 2, ErrorMessage = "First Name must be {2} to {1} characters")]
    public string FirstName { get; set; }
}

:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel
        {
            FirstName = "foo"
        });
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}

:

@model MyViewModel

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        var settngs = $.data($('form')[0], 'validator').settings;
        settngs.onfocusout = function (element) { $(element).valid(); };
    });
</script>

@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.FirstName)
    @Html.ValidationMessageFor(x => x.FirstName)
    <button type="submit">OK</button>
}
+5

All Articles