我有一个包含所有这些字段的表单,并且它们都需要附加验证器。
我的目标是让用户输入表单,如果他填写姓氏并点击提交,表单仍然会回发以保存姓氏,但其他3个字段将显示错误消息“blah blah blah”
基本上我需要验证表单,但即使表单失败,我只想显示错误摘要,但仍然提交有效字段(如果有的话)。
<input runat="server" id="LastName" type="text" name="LastName"/>
<asp:RequiredFieldValidator ID="rvLastName" runat="server" ErrorMessage="Please type last name" ControlToValidate="LastName">Last Name Required</asp:RequiredFieldValidator>
<input runat="server" id="FirstName" type="text" name="FirstName"/>
<required first Name>
<input runat="server" id="MiddleName" type="text" name="MiddleName"/>
<required middle name>
<input runat="server" id="EmailAddress" type="text" name="EmailAddress"/>
<required emailaddress>
<script type="text/javascript">
$(document).ready(function () {
$('#forminfo').validate({
rules: {
LastName: {
required: true
},
EmailAddress: {
required: true,
email: true
}
},
messages: {
LastName: "Please enter your last name",
EmailAddress: "Please enter a valid email address"
}
});
});
</script>
答案 0 :(得分:1)
尝试解释Tim Schmelter建议的内容。
在标记集EnableClientScript = "false"
中:
<input runat="server" id="LastName" type="text" name="LastName"/>
<asp:RequiredFieldValidator ID="rvLastName" runat="server" EnableClientScript="false" ErrorMessage="Please type last name" ControlToValidate="LastName">Last Name Required</asp:RequiredFieldValidator>
<input runat="server" id="FirstName" type="text" name="FirstName"/>
<required first Name>
<input runat="server" id="MiddleName" type="text" name="MiddleName"/>
<required middle name>
<input runat="server" id="EmailAddress" type="text" name="EmailAddress"/>
<required emailaddress>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
我添加了一个提交按钮。在代码中,按钮的事件处理程序,验证页面如下:
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
//Do other tasks
}
else
{
//Do tasks even if validation fails
}
}
希望它有意义!