我有一个使用jQuery Validate执行验证的ASP.NET MVC表单。所有字段都验证正常,除了最后一个名为AmtRemaining'。此字段具有初始值,但此值会根据其他两个字段的输入值而变化(' AmtAdministered'' AmtWasted')。验证此字段是为了确保该值不低于零(使用jQuery Validate中的min:0方法)。
现在,如果我选择" AmtRemaining"字段,然后选项卡,它似乎验证工作。如果该值低于零,它将给我一个错误消息,但是,当我去提交表单时,它允许表单提交,即使该字段未通过验证。它只适用于这个领域。所有其他字段,如果无效,将导致表单不提交。真奇怪。
这是我用于验证的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>
此脚本用于计算&#34; AmtRemaining&#34;的值。基于&#34; AmtAdministered&#34;的输入值和#34; AmtWasted&#34;:
<script type="text/javascript">
$(document).ready(function () {
$('#AmtAdministered, #AmtWasted').blur(function () {
var amtadmin = $('#AmtAdministered').val()
var waste = $('#AmtWasted').val();
var remaining = $('#InitialAmtRemaining').val();
if (!waste)
{
waste = 0;
}
var tmp = (parseFloat(amtadmin) + parseFloat(waste));
var result = (remaining - tmp);
$('#AmtRemaining').val(result);
});
});
</script>
这是View的代码,如果有人有兴趣
@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>
}
任何人都有任何关于为什么允许表单提交无效字段的想法?
答案 0 :(得分:0)
似乎AmtRemaining字段标记为只读。我已经看到许多系统跳过了readonly的验证,因为假设它们不会发生变化。
也许您可以在其他字段上放置onChange方法来拒绝AmtRemaining的值低于零的更改。