jQuery验证允许提交无效字段

jQuery验证允许提交无效字段,jquery,asp.net,asp.net-mvc,unobtrusive-validation,Jquery,Asp.net,Asp.net Mvc,Unobtrusive Validation,我有一个ASP.NETMVC表单,它使用jQuery验证来执行验证。除了最后一个名为“AmtRemaining”的字段外,所有字段都可以正常验证。此字段具有初始值,但此值会根据其他两个字段(“amtAdministrated”和“amtWated”)的值输入而更改。对该字段进行验证是为了确保该值不低于零(使用jQuery Validate中的min:0方法) 现在,如果我在“AmtRemaining”字段上单击tab键,然后在该字段上单击tab键,则验证工作似乎正常。如果该值低于零,它将给我一条

我有一个ASP.NETMVC表单,它使用jQuery验证来执行验证。除了最后一个名为“AmtRemaining”的字段外,所有字段都可以正常验证。此字段具有初始值,但此值会根据其他两个字段(“amtAdministrated”和“amtWated”)的值输入而更改。对该字段进行验证是为了确保该值不低于零(使用jQuery Validate中的min:0方法)

现在,如果我在“AmtRemaining”字段上单击tab键,然后在该字段上单击tab键,则验证工作似乎正常。如果该值低于零,它将给我一条错误消息,但是,当我去提交表单时,它允许表单提交,即使该字段未通过验证。它只适用于这个领域。所有其他字段如果无效,将导致表单不提交。真奇怪

以下是用于验证的jQuery:

<script type="text/javascript">
    $(document).ready(function () {
        $('#DrugAdministerForm').validate({
            errorClass: 'errorText',
            rules: {
                    AdministeredByAutoComplete: {
                            required: true
                        },
                    AmtAdministered: {
                            required: true,
                            range: [0, 100000]
                        },
                    AmtWasted: {
                            range: [0, 100000]
                        },
                    WitnessAutoComplete: {
                            required: {
                                depends: function (element) {
                                    return ($('#AmtWasted').val() != "" &&
                                            $('#AmtWasted').val() != "0");
                                }
                            }
                        },
                    ProviderList: {
                            required:  true
                        },
                    AmtRemaining: {
                            min: 0                              
                        }
                },
            messages: {
                    AmtAdministered: {
                            required: "Required",
                            range: "Not a valid amount"
                        },
                    AmtWasted: {
                            range:  "Not a valid amount"
                        },
                    WitnessAutoComplete:{
                            required: "Required if waste"
                        },
                    AmtRemaining: {
                            min: "Must be greater than 0"
                        }
                }
                });
    });

</script>

$(文档).ready(函数(){
$(“#DrugAdministerForm”)。验证({
errorClass:'errorText',
规则:{
由自动完成管理:{
必填项:true
},
提供服务的国家:{
要求:正确,
范围:[0,100000]
},
AMSWASTED:{
范围:[0,100000]
},
见证自动完成:{
所需:{
依赖:函数(元素){
返回($('#AmtWasted').val()!=“”&&
$('#AmtWasted').val()!=“0”);
}
}
},
提供者列表:{
必填项:true
},
AmtRemaining:{
最低:0
}
},
信息:{
提供服务的国家:{
必选:“必选”,
范围:“不是有效金额”
},
AMSWASTED:{
范围:“不是有效金额”
},
见证自动完成:{
必需:“如果是废物,则必需”
},
AmtRemaining:{
最小值:“必须大于0”
}
}
});
});
此脚本用于根据“amtAdministrated”和“amtWated”的输入值计算“amtMaining”的值:


$(文档).ready(函数(){
$('#amtAdministrated,#amtWated').blur(函数(){
var amtadmin=$('#AmtAdministered').val()
var waste=$('#AmtWasted').val();
剩余变量=$('InitialAmtRemaining').val();
如果(!浪费)
{
废物=0;
}
var tmp=(parseFloat(amtadmin)+parseFloat(waste));
var结果=(剩余-tmp);
$('#AmtRemaining').val(结果);
});
});
这是视图的代码,如果有人感兴趣的话

@using (Html.BeginForm("Add", "Patient", FormMethod.Post, new { id = "DrugAdministerForm" }))
{
    <br />
    <table class="Tbl">
        <tr>
            <td class="right">Lot #:  </td>
            <td class="left">@Html.TextBox("LotNumberReadOnly", @Model.SelectedLot.LotNumber, new { @readonly = "readonly", @class = "TxtBoxGray" })</td>
        </tr>
        <tr>
            <td class="right">Drug Name/Type:  </td>
            <td class="left">
                @Html.TextBox("DrugNameReadOnly", @Model.SelectedLot.DrugName, new { @readonly = "readonly", @class = "TxtBoxGray", @style = "width:400px" })
                @Html.Hidden("DrugEntryCodeHidden", @Model.SelectedLot.DrugEntryCode)
            </td>
        </tr>
        <tr>
            <td class="right">Site:</td>
            <td class="left">
                @Html.TextBox("SiteNameReadOnly", @Model.SelectedLot.SiteName, new { @readonly = "readonly", @class = "TxtBoxGray", @style = "width:400px" })
                @Html.Hidden("SiteNumberHidden", @Model.SelectedLot.SiteNumber)
            </td>
        </tr>
        <tr>
            <td class="right">Patient MRN:  </td>
            <td class="left">@Html.TextBox("PatientMrnReadOnly", @Model.Patient.Mrn, new { @readonly = "readonly", @class = "TxtBoxGray" })</td>
        </tr>
        <tr>
            <td class="right">Patient Name:  </td>
            <td class="left">@Html.TextBox("PatientNameReadOnly", @Model.Patient.FullNameLastNameFirst, new { @readonly = "readonly", @class = "TxtBoxGray" })</td>
        </tr>
        <tr>
            <td class="right">Date of Birth:  </td>
            <td class="left">@Html.TextBox("PatientDobReadOnly", @Model.Patient.DateOfBirth, new { @readonly = "readonly", @class = "TxtBoxGray" }) </td>
        </tr>
        <tr>
            <td class="right">Provider:  </td>
            <td class="left">@Html.DropDownList("ProviderList", "Choose...")</td>
        </tr>
        <tr>
            <td class="right">Administered By:  </td>
            <td class="left">@(Html.Kendo().AutoComplete().Name("AdministeredByAutoComplete").DataTextField("UserName").BindTo(Model.UserNames).Filter("contains")
                                            .HtmlAttributes(new { value = (string)ViewBag.LoggedInUserName, 
                                            style = "padding:0px; font-size:10pt; border-radius:0; color:#000; line-height:1em;" }))</td>
        </tr>
        <tr>
            <td class="right">Amount Administered (CCs):  </td>
            <td class="left">@Html.TextBox("AmtAdministered", null, new { @id = "AmtAdministered", @type = "text", @style = "width:30px"})</td>
        </tr>
        <tr>
            <td class="right">Amount Wasted (CCs):  </td>
            <td class="left">@Html.TextBox("AmtWasted", null, new { @style = "width:30px"})</td>
        </tr>
        <tr>
            <td class="right">Waste Witnessed By:  </td> 
            <td class="left">@(Html.Kendo().AutoComplete().Name("WitnessAutoComplete").DataTextField("UserName").BindTo(Model.UserNames).Filter("contains")
                                            .HtmlAttributes(new { style = "padding:0px; font-size:10pt; border-radius:0; color:#000; line-height:1em;" }))</td>
        </tr>
        <tr>
            <td class="right">Amount Remaining (Lot):  </td>
            <td class="left">@Html.TextBox("AmtRemaining", @Model.SelectedLot.CCsRemaining, new { @style = "width:50px", @readonly = "readonly", @class = "TxtBoxGray" })</td>
        </tr>
        <tr>
            <td class="right">@Html.Hidden("InitialAmtRemaining", @Model.SelectedLot.CCsRemaining)</td>
            <td class="left"><input type="submit" value="Record" /></td>
        </tr>
    </table>

}
@使用(Html.BeginForm(“Add”,“Patient”,FormMethod.Post,new{id=“DrugAdministerForm”}))
{

地段: @TextBox(“lotnumberradonly”,@Model.SelectedLot.LotNumber,new{@readonly=“readonly”,@class=“TxtBoxGray”}) 药物名称/类型: @Html.TextBox(“DrugNameReadOnly”、@Model.SelectedLot.DrugName,new{@readonly=“readonly”、@class=“TxtBoxGray”、@style=“width:400px”}) @Html.Hidden(“DrugEntryCodeHidden”,@Model.SelectedLot.DrugEntryCode) 地点: @TextBox(“SiteNameReadOnly”,@Model.SelectedLot.SiteName,new{@readonly=“readonly”,@class=“TxtBoxGray”,@style=“width:400px”}) @Html.Hidden(“SiteNumberHidden”,@Model.SelectedLot.SiteNumber) 患者MRN: @TextBox(“PatientMrnReadOnly”,@Model.Patient.Mrn,new{@readonly=“readonly”,@class=“TxtBoxGray”}) 病人姓名: @TextBox(“PatientNameReadOnly”,@Model.Patient.FullNameLastNameFirst,新的{@readonly=“readonly”,@class=“TxtBoxGray”}) 出生日期: @TextBox(“PatientDobReadOnly”,@Model.Patient.DateOfBirth,new{@readonly=“readonly”,@class=“TxtBoxGray”}) 供应商: @下拉列表(“ProviderList”,“Choose…”) 管理人: @(Html.Kendo().AutoComplete().Name(“AdministedByAutoComplete”).DataTextField(“用户名”).BindTo(Model.UserNames).过滤器(“包含”) .HtmlAttributes(新的{value=(字符串)ViewBag.LoggedInUserName, style=“padding:0px;font-size:10pt;边框半径:0;颜色:#000;线条高度:1em;”})) 管理金额(CCs): @TextBox(“amtadministrated”,null,新的{@id=“amtadministrated”,@type=“text”,@style=“width:30px”}) 浪费量(CCs): @TextBox(“AmtWasted”,null,新的{@style=“width:30px”}) 废物见证人: @(Html.Kendo().AutoComplete().Name(“见证自动完成”).DataTextField(“用户名”).BindTo(Model.UserNames).过滤器(“包含”) .HtmlAttributes(新的{style=“padding:0px;字体大小:10
@using (Html.BeginForm("Add", "Patient", FormMethod.Post, new { id = "DrugAdministerForm" }))
{
    <br />
    <table class="Tbl">
        <tr>
            <td class="right">Lot #:  </td>
            <td class="left">@Html.TextBox("LotNumberReadOnly", @Model.SelectedLot.LotNumber, new { @readonly = "readonly", @class = "TxtBoxGray" })</td>
        </tr>
        <tr>
            <td class="right">Drug Name/Type:  </td>
            <td class="left">
                @Html.TextBox("DrugNameReadOnly", @Model.SelectedLot.DrugName, new { @readonly = "readonly", @class = "TxtBoxGray", @style = "width:400px" })
                @Html.Hidden("DrugEntryCodeHidden", @Model.SelectedLot.DrugEntryCode)
            </td>
        </tr>
        <tr>
            <td class="right">Site:</td>
            <td class="left">
                @Html.TextBox("SiteNameReadOnly", @Model.SelectedLot.SiteName, new { @readonly = "readonly", @class = "TxtBoxGray", @style = "width:400px" })
                @Html.Hidden("SiteNumberHidden", @Model.SelectedLot.SiteNumber)
            </td>
        </tr>
        <tr>
            <td class="right">Patient MRN:  </td>
            <td class="left">@Html.TextBox("PatientMrnReadOnly", @Model.Patient.Mrn, new { @readonly = "readonly", @class = "TxtBoxGray" })</td>
        </tr>
        <tr>
            <td class="right">Patient Name:  </td>
            <td class="left">@Html.TextBox("PatientNameReadOnly", @Model.Patient.FullNameLastNameFirst, new { @readonly = "readonly", @class = "TxtBoxGray" })</td>
        </tr>
        <tr>
            <td class="right">Date of Birth:  </td>
            <td class="left">@Html.TextBox("PatientDobReadOnly", @Model.Patient.DateOfBirth, new { @readonly = "readonly", @class = "TxtBoxGray" }) </td>
        </tr>
        <tr>
            <td class="right">Provider:  </td>
            <td class="left">@Html.DropDownList("ProviderList", "Choose...")</td>
        </tr>
        <tr>
            <td class="right">Administered By:  </td>
            <td class="left">@(Html.Kendo().AutoComplete().Name("AdministeredByAutoComplete").DataTextField("UserName").BindTo(Model.UserNames).Filter("contains")
                                            .HtmlAttributes(new { value = (string)ViewBag.LoggedInUserName, 
                                            style = "padding:0px; font-size:10pt; border-radius:0; color:#000; line-height:1em;" }))</td>
        </tr>
        <tr>
            <td class="right">Amount Administered (CCs):  </td>
            <td class="left">@Html.TextBox("AmtAdministered", null, new { @id = "AmtAdministered", @type = "text", @style = "width:30px"})</td>
        </tr>
        <tr>
            <td class="right">Amount Wasted (CCs):  </td>
            <td class="left">@Html.TextBox("AmtWasted", null, new { @style = "width:30px"})</td>
        </tr>
        <tr>
            <td class="right">Waste Witnessed By:  </td> 
            <td class="left">@(Html.Kendo().AutoComplete().Name("WitnessAutoComplete").DataTextField("UserName").BindTo(Model.UserNames).Filter("contains")
                                            .HtmlAttributes(new { style = "padding:0px; font-size:10pt; border-radius:0; color:#000; line-height:1em;" }))</td>
        </tr>
        <tr>
            <td class="right">Amount Remaining (Lot):  </td>
            <td class="left">@Html.TextBox("AmtRemaining", @Model.SelectedLot.CCsRemaining, new { @style = "width:50px", @readonly = "readonly", @class = "TxtBoxGray" })</td>
        </tr>
        <tr>
            <td class="right">@Html.Hidden("InitialAmtRemaining", @Model.SelectedLot.CCsRemaining)</td>
            <td class="left"><input type="submit" value="Record" /></td>
        </tr>
    </table>

}