asp:LinkButton -验证表单字段

本文关键字:表单 字段 验证 LinkButton asp | 更新日期: 2023-09-27 18:02:11

我对c#完全陌生,我正试图弄清楚表单验证。更具体地说,我有一个webform (c#),我把它分成了"form-part-1"answers"form-part-2"两个部分。

默认情况下,form-part-2是隐藏的。当"表格-1"的所有栏位填妥后,你可以按"继续"连结按钮继续"表格-2"。

jQuery部分工作得很好,可以按需要显示和隐藏表单部分。但是验证不再强制执行。

我已经准备好了验证器,但是现在我可以继续"form-part-2",而不需要填写"form-part-1"字段。

我希望在进行"form-part-2"之前执行验证。

如果有什么建议,我将不胜感激。

提前感谢。

下面是我的代码:

    <script language="javascript" type="text/javascript">
    $(document).ready(function () {
            $("#continue").click(function (event) {
                $('#form-part-1').hide();
                $('#form-part-2').fadeIn();
            });
    });
</script>
<form id="SignUp" method="post" runat="server">
<table id="validation">
    <tr>
        <td colspan="2" vAlign="top">
        <asp:ValidationSummary ID="vsSignupValidation" runat="server"></asp:ValidationSummary></td>
    </tr>
</table>
<table id="form-part-2">
<tr>
    <td width="150">
            <label class="">
            <asp:label id="lblSignupFirstName" Runat="server">First Name:</asp:label>
            <span style="color:red">*</span>
            </label>
    </td>
    <td>
      <asp:TextBox size="30" CssClass="input" ID="txtSignupFirstName" TabIndex="1" runat="server"></asp:TextBox>
    </td>
</tr>
<tr>
    <td width="150">
            <label class="">
            <asp:label id="lblSignupLastName" Runat="server">Last Name:</asp:label>
            <span style="color:red">*</span>
            </label>
    </td>
    <td>
      <asp:TextBox size="30" CssClass="input" ID="txtSignupLastName" TabIndex="1" runat="server"></asp:TextBox>
    </td>
</tr>
</table>
<!-- validate and continue -->
    <asp:LinkButton ID="continue" runat="server"  onclientclick="return false;">Continue</asp:LinkButton>
<!-- validate and continue -->
<table id="form-part-1">
<tr>
    <td width="150">
            <label class="">
            <asp:label id="lblSignupUserID" Runat="server">UserID:</asp:label>
            <span style="color:red">*</span>
            </label>
    </td>
    <td>
      <asp:TextBox size="30" CssClass="input" ID="txtSignupUserID" TabIndex="1" runat="server"></asp:TextBox>
    </td>
</tr>
<tr>
    <td width="150">
        <label class="">
            <asp:label id="lblSignupPassword" Runat="server">Last Name:</asp:label>
            <span style="color:red">*</span>
        </label>
    </td>
    <td>
      <asp:TextBox size="30" CssClass="input" ID="txtSignupPassword" TabIndex="1" runat="server"></asp:TextBox>
    </td>
</tr>
<tr>
    <td width="150">
        <label class="">
            <asp:Label class="formtxtsm" ID="lblSignupConfirmPassword" runat="server">Confirm password</asp:Label>
            <span style="color:red">*</span>
        </label></td>
    <td>
        <asp:TextBox size="30" ID="txtSignupConfirmPassword" TabIndex="10" runat="server" TextMode="Password" CssClass="input"></asp:TextBox>
    </td>
</tr>
</table>
<!-- validators -->
<asp:requiredfieldvalidator id="rfvSignupFirstName" runat="server" Display="None" ControlToValidate="txtSignupFirstName" class="formerrortxt" ErrorMessage='"First Name" is required'></asp:requiredfieldvalidator>
<asp:requiredfieldvalidator id="rfvSignupLastName" runat="server" Display="None" ControlToValidate="txtSignupLastName" class="formerrortxt" ErrorMessage='"Last Name" is required'></asp:requiredfieldvalidator>
<asp:RequiredFieldValidator ID="rfvSignupUserID" runat="server" Display="None" ControlToValidate="txtSignupUserID" ErrorMessage='"Username" is required.'></asp:RequiredFieldValidator>
<asp:requiredfieldvalidator id="rfvSignupPassword" runat="server" Display="None" ControlToValidate="txtSignupPassword" ErrorMessage='"Password" is required.'></asp:requiredfieldvalidator>
<asp:requiredfieldvalidator id="rfvSignupConfirmPassword" runat="server" Display="None" ControlToValidate="txtSignupConfirmPassword" ErrorMessage='"Confirm password" is required.'></asp:requiredfieldvalidator>
<asp:customvalidator id="cvSignupPasswordMatch" runat="server" Display="None" ErrorMessage='"Password" and "Confirm password" must match exactly.'></asp:customvalidator>
</form>

* * * * 编辑:

谢谢Phx &丹尼尔,谢谢你的反馈。很有帮助!一切都很顺利,只有一个例外。在进入步骤2之前,"form-part-2"字段就得到了验证。如何验证用户名/密码字段只有在我得到"form-part-2"吗?提前感谢!

这是我的最新版本:www.smithy.somee.com

和代码:

<script language="javascript" type="text/javascript">
$(document).ready(function () {            
    if (Page_ClientValidate("personalGroup")) {
        $('#form-part-1').hide();
        $('#form-part-2').fadeIn();
    }            
    if (Page_ClientValidate("accountGroup")) {                    
        $('#form-part-2').hide();
    }
});
</script>
    <form id="signup" runat="server">
    <div>
        <table id="validators">
            <tr>               
                <td>
                    <asp:ValidationSummary ID="personalGroupSummary" runat="server" ValidationGroup="personalGroup" />
                    <asp:ValidationSummary ID="accountGroupSummary" runat="server"  ValidationGroup="accountGroup" />
                </td>
            </tr>
        </table>
        <table id="form-part-1">
            <tr>
                <td>First Name:</td>
                <td><asp:TextBox ID="txtFname" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Last Name:</td>
                <td><asp:TextBox ID="txtLname" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td></td>
                <td>                       
                    <asp:Button ID="continue" runat="server" causesvalidation="true" validationgroup="personalGroup"  Text="Continue"  />
                </td>
            </tr>
        </table>

        <table id="form-part-2">
            <tr>
                <td>Username:</td>
                <td><asp:TextBox ID="txtUser" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><asp:TextBox ID="txtPass" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td></td>
                <td>                                      
                    <asp:Button ID="btnSubmit" runat="server" validationgroup="accountGroup" Text="Submit" OnClick="btnSubmit_Click" />
                </td>
            </tr>
        </table>
        <!-- output -->
        <table>
            <tr>
                <td>First: </td>
                <td><asp:Label ID="lblFname" runat="server" Text=""></asp:Label></td>
            </tr>
            <tr>
                <td>Last:</td>
                <td><asp:Label ID="lblLname" runat="server" Text=""></asp:Label></td>
            </tr>
            <tr>
                <td>User:</td>
                <td><asp:Label ID="lblUser" runat="server" Text=""></asp:Label></td>
            </tr>
            <tr>
                <td>Pass:</td>
                <td><asp:Label ID="lblPass" runat="server" Text=""></asp:Label></td>
            </tr>
        </table>
    </div>
        <!-- validators -->
            <asp:requiredfieldvalidator id="fvFname" runat="server" validationgroup="personalGroup" Display="None" ControlToValidate="txtFname" ErrorMessage='"First Name" is required'></asp:requiredfieldvalidator>
            <asp:requiredfieldvalidator id="fvLname" runat="server" validationgroup="personalGroup"  Display="None" ControlToValidate="txtLname" ErrorMessage='"Last Name" is required'></asp:requiredfieldvalidator>
            <asp:requiredfieldvalidator id="fvUser" runat="server" validationgroup="accountGroup" Display="None" ControlToValidate="txtUser" ErrorMessage='"Username" is required'></asp:requiredfieldvalidator>
            <asp:requiredfieldvalidator id="fvPass" runat="server" validationgroup="accountGroup" Display="None" ControlToValidate="txtPass" ErrorMessage='"Password" is required'></asp:requiredfieldvalidator>
        <!-- validators -->
    </form>

asp:LinkButton -验证表单字段

正如您所写的:我希望在进行"form-part-2"之前强制执行验证。

您需要创建验证组来验证前N个字段和另外N个字段。因此,为控件创建验证组:

http://msdn.microsoft.com/en-us/library/vstudio/ms227424 (v = vs.100) . aspx

<asp:textbox id="AgeTextBox" 
      runat="Server">
    </asp:textbox>
<asp:requiredfieldvalidator id="RequiredFieldValidator2"
      controltovalidate="AgeTextBox"
      validationgroup="PersonalInfoGroup"
      errormessage="Enter your age."
      runat="Server">
    </asp:requiredfieldvalidator>
<!--When Button1 is clicked, only validation
    controls that are a part of PersonalInfoGroup
    are validated.-->
    <asp:button id="Button1" 
      text="Validate" 
      causesvalidation="true"
      validationgroup="PersonalInfoGroup"
      runat="Server" />

<asp:textbox id="CityTextBox" 
      runat="Server">
    </asp:textbox>

    <asp:requiredfieldvalidator id="RequiredFieldValidator3"
      controltovalidate="CityTextBox"
      validationgroup="LocationInfoGroup"
      errormessage="Enter a city name."
      runat="Server">
    </asp:requiredfieldvalidator>

    <!--When Button2 is clicked, only validation
    controls that are a part of LocationInfoGroup
    are validated.-->
    <asp:button id="Button2" 
      text="Validate" 
      causesvalidation="true"
      validationgroup="LocationInfoGroup"
      runat="Server" />

然后对每个组使用不同的验证摘要。

也许你需要一个按钮为每个验证,但你可以这样做,并使用相同的按钮为多个验证组:

http://www.aspsnippets.com/Articles/Validate-Multiple-Validation-Groups-with-one-Button-in-ASPNet.aspx

另一个例子http://www.codeproject.com/Tips/401611/Validate-multiple-Validation-group-both-client-and

Phx给出了部分答案。使用他的建议来创建ValidationGroups。将第1部分和第2部分中的所有验证器放在不同的组中。

JQuery不自动触发验证

您需要用JavaScript手动调用验证。这可以通过调用Page_ClientValidate来完成,它将验证传递的ValidationGroup的所有验证器。

例如:

$("#continue").click(function (event) {
   if (Page_ClientValidate("group1")) {
      $('#form-part-1').hide();
      $('#form-part-2').fadeIn();
   }
});