使用MVC 4的JQuery验证未验证模糊

时间:2013-07-31 08:04:44

标签: asp.net-mvc jquery-validation-engine

我用MVC 4和jQuery验证在墙上敲了几个小时,以验证第一个关于模糊的问题。我已经尝试将验证附加到整个表单和单个元素(第一个字段)无济于事。如果我在模糊事件中添加一个alert(),它似乎会触发,但不会验证所需的字段。我需要在需要的工作之后添加其他验证,但还没有完成。我不知道它是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>

1 个答案:

答案 0 :(得分:0)

你能试试吗?

var settngs = $.data($('#myForm')[0], 'validator').settings;
settngs.onkeyup = false;