0

我有一个包含所有这些字段的表单,并且它们都附加了必需的验证器。

我的目标是让用户输入表单,如果他填写姓氏并点击提交,表单仍将回发以保存姓氏,但其他 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>
4

1 回答 1

1

试图解释蒂姆施梅尔特的建议。

在标记集中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
    }
}

希望这是有道理的!

于 2013-08-20T23:32:48.500 回答