0

我用 MVC 4 和 jQuery 验证在墙上敲了几个小时,以验证第一次模糊。我尝试将验证附加到整个表单和单个元素(第一个字段),但无济于事。如果我将 alert() 添加到 blur 事件中,它似乎会触发但未验证必填字段。在所需的工作正常但还没有得到它们之后,我有额外的验证要添加。我不知道这是 MVC 4 的问题。JQueryvalidate 是 v1.10。我还尝试设置验证器,然后在要使用 .blur 验证的元素上调用 .valid() ,但仍然无法看到我可以看到的验证。

$(function () {
    $('#productionOrder').focus();

    $.validator.addMethod("cMinLength", $.validator.methods.minlength,
        $.format("Must contain as least {0} chars"));
    $.validator.addClassRules("productionOrder", { cMnLength: 3 });

    $('#myForm').validate({
        onkeyup: false,
        //onfocusout: false,
        errorClass: 'fieldError'
        //rules: {
        //    productionOrder: "required number",
        //    tc: "required",
        //    dn: "required"
        //}
    });

    $("#towCount").bind("change keyup", function () {
        $form.validate().element("#towCount");
    });

    //$('#productionOrder').validate({
    //    //onkeyup: false,
    //    onfocusout: false,
    //    errorClass: 'fieldError',
    //    rules: {
    //        productionOrder: { 
    //            required: true 
    //        }
    //    }
    //});
});

和 .cshtml

@model FiberLine2.ViewModels.Creel
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Test</title>
    <script src="~/Scripts/jquery-1.8.2.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <style>
        .fieldError {
            border-color: red;
            border-width: medium;
        }

        .input-label {
            font-size: 13px;
            width: 130px;
            height: 30px;
            display: inline-block;
        }
    </style>
</head>
<body>
    <form id="myForm">
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <div>
            <!-- app header -->
            <label>@Resources.Strings.User: </label>
            <label>@User.Identity.Name</label>
        </div>
        <fieldset>
            <legend>@Resources.Strings.Creel @Resources.Strings.Load</legend>
            <div>
                <div id="errorDiv"></div>
                <hr />
                @Html.Label(@Resources.Strings.ProductionOrder, new { @class = "input-label lock" })
                <input type="text" id="productionOrder" name="productionOrder" class="required" maxlength="4" />
                <br />
                <label class="input-label lock">@Resources.Strings.Tow  @Resources.Strings.Count</label>
                <input type="text" id="towCount" name="tc" class="required" size="5" maxlength="5" value="299" />
                <br />
                <label class="input-label lock">@Resources.Strings.Batch @Resources.Strings.Sequence</label>
                <input type="text" id="doffNumber" name="dn" size="5" maxlength="5" value="1" />
                <br />
                <label class="input-label">@Resources.Strings.Creel @Resources.Strings.Position</label>
                <input type="text" id="creelPosition" name="cp" size="5" maxlength="5" />
                <br />
                <label class="input-label">@Resources.Strings.Batch @Resources.Strings.ID</label>
                <input type="text" id="creelNumber" name="cn" size="7" maxlength="7" />
                <br />
                <hr />
            </div>
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    </form>
</body>
</html>
4

1 回答 1

0

你可以试试这个吗?

var settngs = $.data($('#myForm')[0], 'validator').settings;
settngs.onkeyup = false;
于 2013-08-01T15:38:48.843 回答