form.valid()始终返回真实的jQuery验证

时间:2019-06-14 01:27:54

标签: jquery asp.net-mvc

我有一个表单,它具有使用视图模型设置的几个必填字段。验证可以很好地用于新表单/创建选项。当我尝试验证表单的编辑选项时,它总是返回true。即使未填写必填字段,它也会返回true。

我已经搜索并尝试了其他stackoverflow中提到的类似问题的解决方案。仍然我的反应和真相一样

  1. jquery validation valid() always returns true and form's novalidate="novalidate" property is not created

  2. jQuery Validation always returns true

  3. jquery validate always returning true

  4. jQuery Validation - form.valid() always returning true

<!-- create dialog -->
@using (var modal = Html.SmartAdmin().Begin(new Modal(new { id = "save", @class = "fade appendThis" },
new ModalSettings()
{
    Title = "Info",
    Buttons = new MvcHtmlString[]
{
                Html.SmartAdmin().ActionButton("OK", string.Empty, BootstrapColor.Primary, new { id="btnCreate" })
},
    CancelButtonText = "Cancel",
    ModalSize = BootstrapSizes.Large
})))
                    {
@Html.Partial("~/area/_Save.cshtml", new SaveViewModel())
                    }

                    <!--Edit Dialog-->
                    @using (var modal = Html.SmartAdmin().Begin(new Modal(new { id = "edit", @class = "fade appendThis" },
new ModalSettings()
{
    Title = "Info",
    Buttons = new MvcHtmlString[]
{
                Html.SmartAdmin().ActionButton("OK", string.Empty, BootstrapColor.Primary, new { id="btnEdit" })
},
    CancelButtonText = "Cancel",
    ModalSize = BootstrapSizes.Large
})))
                    {

                        @Html.Partial("~/areas/_Save.cshtml", new SaveViewModel())
                    }


                }

表格:

<form id="frmSave">
                <fieldset>
                    <legend>

                    </legend>
                    <input type="hidden" name="hid" id="hID" />
                    <div class="form-group">
                        <div class="row margin-bottom-15">
                            <div class="col-lg-6">
                                <label for="Name" class="control-label requiredlabel">
                                    Name
                                </label>
                                <input id="Name" name="Name" class="form-control" type="text" />
                            </div>

                            <div class="col-lg-6">
                                <label class="control-label requiredlabel" for="Type">Type</label>
                                <select id="Type" name="Type" class="form-control">
                                    @foreach (var currentType in Model.Types)
                                    {
                                        <option value="@currentType.ID.Value">@currentType.Name</option>
                                    }
                                </select>
                            </div>
                        </div>

                        <div class="row margin-bottom-15">
                            <div class="col-lg-3">
                                <label for="Units" class="control-label requiredlabel">
                                    Units
                                </label>
                                <input id="Units" name="Units" class="form-control" onkeydown="PreventLetters();" type="number" min="0" />
                            </div>
                            <div class="col-lg-3">
                                <label for="DataUnit" class="control-label requiredlabel">
                                    Data Unit
                                </label>
                                <input id="DataUnit" name="DataUnit" class="form-control" onkeydown="PreventLetters();" type="number" />
                            </div>
                        </div>

                        <div class="row padding-top-10 margin-bottom-15">

                            <div class="col-lg-6">
                                <div class="form-group">
                                    <label class="requiredlabel">Start Date</label>
                                    <div class="input-group">
                                        <input id="StartDate" type="text" name="StartTimeStamp" placeholder="Select a date" class="form-control datepicker">
                                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                    </div>
                                </div>
                                <label for="StartDate" generated="true" class="error"></label>
                            </div>
                        </div>
                    </div>
                </fieldset>
            </form>

验证码:

 $('#frmSave').validate({
        rules: {
            Name: {
                required: true
            },
            Type: {
                required: true
            },
            Units: {
                required: true,
                number:true
            },
            DataUnit: {
                required: true,
                number:true
            },
            StartTimeStamp: {
                required: true
            },
        },

        highlight: function (element) {
            $(element).css('background', '#ffdddd');
        },

        unhighlight: function (element) {
            $(element).css('background', '#ffffff');
        }
    });

脚本:

$('#btnCreate').on("click", function (e) {
            var status = $("#frmSave").valid();
            alert("Status: " + status);
        });

$('#btnEdit').on("click", function (e) {
            var status = $("#frmSave").valid();
            alert("Status: " + status);
        });

如果未填写必填字段,则在create选项中我将获得为false的有效身份。但是,对于编辑,它总是返回true。

0 个答案:

没有答案