jQuery验证总是返回true

jQuery验证总是返回true

本文介绍了jQuery验证总是返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有JQuery的MVC4,我有一个使用@ Ajax.BeginForm创建的表单.它具有必填字段(在其视图模型上设置).我正在尝试验证表单,然后再将其提交回去.但是,它似乎没有验证.

I am using MVC4 w/JQuery and I have a form I created using the @Ajax.BeginForm. It has required fields (set on its view model). I am trying to validate the form before it is submitted back; however, it does not seem to validate.

我最初有一个提交按钮,它只是在提交表单.我将提交按钮更改为常规按钮,并尝试调用$('#formname').validate(),但它始终返回true(即使未填写必填字段).其他视图似乎运行良好(不使用Ajax.BeginForm).

I had a submit button originally and it was just submitting the form. I changed the submit button to a regular button and tried calling $('#formname').validate() but it always returns true (even tho required fields are not filled in). Other views seem to work fine (that are not using the Ajax.BeginForm).

我正在引用jquery,jquery-ui,jquery.validate,jquery.validate.unobtrusive和jquery.unobtrusive-ajax.

I am referencing jquery, jquery-ui, jquery.validate, jquery.validate.unobtrusive, and jquery.unobtrusive-ajax.

有人对我有什么想法/建议吗?

Anyone have any ideas/suggestions what I am missing?

edit ---------------------------------------------- -----------------------------

edit---------------------------------------------------------------------------

下面是我的代码(这是在引用jquery脚本的母版页中):

Below is my code (this is inside a master page that references the jquery scripts):

@model AimOnlineMRA.Models.Reports.DateRangeViewModel

@using (Ajax.BeginForm("GetReport", "Reports", new AjaxOptions
{
    UpdateTargetId = "report_pane", HttpMethod = "Post"}, new {@id="parameterForm"}))
{
    @Html.AntiForgeryToken()

<script type="text/javascript">
    $(document).ready(function () {
        ApplyTheme();

        $('#btnYes').click(function() {
            $('#RunSpecificDateRange').val('true');

            $('#yesNo').hide();
            $('#dateRangeForm').show();
        });

        $('#btnCancel').click(function () {
            $('#report_pane').html('').hide();
        });

        $('#btnOk').click(function () {
            //if ($('#parameterForm').valid()) {
            //    alert('valid');
            //} else {
            //    alert('invalid');
            //}
        });

        $('#dateRangeForm').hide();

        $('#BeginDate').datepicker({
            showOn: 'button',
            buttonText: 'Date Picker',
            buttonImageOnly: true,
            buttonImage: '@Url.Content("~/Content/Icons/calendar-icon.png")'
        });

        $('#EndDate').datepicker({
            showOn: 'button',
            buttonText: 'Date Picker',
            buttonImageOnly: true,
            buttonImage: '@Url.Content("~/Content/Icons/calendar-icon.png")'
        });
    });
</script>

<div class="margin-auto" style="width: 400px">

    <div id="yesNo">
        <span class="bold">Do you want to run this report for a specific date range?</span>
        <br /><br />
        @Html.Button("Yes", "btnYes")
        @Html.Button("No", "btnNo")
    </div>
    <div id="dateRangeForm">
        <span class="bold">Date Range</span>
        <br /><br />
        <div class="left">
            <div class="left clear-both">
                @Html.LabelFor(x => x.BeginDate) <br />
                @Html.TextBoxFor(x => x.BeginDate, new {@style="vertical-align:top"})
                @Html.ValidationMessageFor(x => x.BeginDate)
            </div>
            <div class="left clear-both m-top-5">
                @Html.LabelFor(x => x.EndDate) <br />
                @Html.TextBoxFor(x => x.EndDate, new {@style="vertical-align:top"})
                @Html.ValidationMessageFor(x => x.EndDate)
            </div>
        </div>
        <div class="left m-left-10">

        </div>
        <div class="left clear-both m-top-10">
            @Html.Button("Ok", "btnOk")
            @Html.Button("Cancel", "btnCancel")
        </div>
    </div>
</div>
}

----------------- RESOLUTION -----------------------------

-----------------RESOLUTION-----------------------------

$('#parameterForm').valid();之前致电$.validator.unobtrusive.parse($('#parameterForm'));解决了该问题

calling $.validator.unobtrusive.parse($('#parameterForm')); before $('#parameterForm').valid(); fixed the issue

推荐答案

尝试

$.validator.unobtrusive.parse($('#parameterForm'))

致电

$('#parameterForm').valid()

这篇关于jQuery验证总是返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:49