验证失败时如何停止重定向到下一页?

时间:2016-09-24 11:50:12

标签: asp.net

我已经在ASP.NET中的Form上对TextBox等控件进行了验证。验证失败时,它仍然会重定向到下一页。我该如何解决这个问题?

<div class="inp-cont forget-success-hide">
   <label for="inputFirstName">Name <span class="text-error"
      style="Color:Red">*</span></label>
   <asp:TextBox runat="server" class="signininput" ID="txtname" placeholder="Enter Your
      Name"></asp:TextBox>
   <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ForeColor="Red"
      ValidationGroup="rg" ErrorMessage="Please enter Your Name"
      ControlToValidate="txtname"></asp:RequiredFieldValidator>
   <br/>
   <asp:RegularExpressionValidator ID="RegularExpressionValidator3"
      runat="server" ControlToValidate="txtname" ErrorMessage="Only
      alphabets are allowed" ForeColor="Red"
      ValidationExpression="[a-zA-Z]+" > </asp:RegularExpressionValidator>
</div>
<div class="inp-cont forget-success-hide">
   <label for="inputFirstName">Email <span class="text-error"
      style="Color:Red">*</span></label>
   <asp:TextBox runat="server" class="signininput" ID="txtemail" placeholder="Enter Your Email
      Id"></asp:TextBox>
   <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ForeColor="Red"
      ValidationGroup="rg" ErrorMessage="Please enter email id"
      ControlToValidate="txtemail"></asp:RequiredFieldValidator>
   <br />
   <asp:RegularExpressionValidator ID="rv" runat="server"
      ControlToValidate="txtemail" ForeColor="Red" ValidationGroup="rg"
      ErrorMessage="Enter vaild email id"
      ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
   <asp:Label ID="lblemail" ForeColor="Red" runat="server" Visible="false"></asp:Label>
</div>
<div class="inp-cont forget-success-hide">
   <label for="inputLastName">Gender <span class="text-error"
      style="Color:Red">*</span></label>
   <asp:DropDownList ID="ddlGender" runat="server" class="signininput" Width="100%">
      <asp:ListItem Text="Select Gender" Value="0"></asp:ListItem>
      <asp:ListItem Text="Male" Value="1"></asp:ListItem>
      <asp:ListItem Text="Female" Value="2"></asp:ListItem>
   </asp:DropDownList>
   <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ForeColor="Red"
      ValidationGroup="rg" ErrorMessage="Please select gender"
      InitialValue="0"
      ControlToValidate="ddlGender"></asp:RequiredFieldValidator>
</div>
<div class="inp-cont forget-success-hide">
   <label for="inputEMail">Password <span class="text-error"
      style="Color:Red">*</span></label>
   <asp:TextBox runat="server" class="signininput" ID="txtpwd" MaxLength="20" TextMode="Password"
      placeholder="Enter Your Password"></asp:TextBox>
   <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ForeColor="Red"
      ValidationGroup="rg" ErrorMessage="Please enter password" 
      ControlToValidate="txtpwd"></asp:RequiredFieldValidator>
</div>
<div class="inp-cont forget-success-hide">
   <label for="inputEMail">Confirm Password <span class="text-error"
      style="Color:Red">*</span></label>
   <asp:TextBox runat="server" class="signininput" ID="txtcpwd" MaxLength="20" TextMode="Password"
      placeholder="Enter Confirm Password"></asp:TextBox>
   <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ForeColor="Red"
      ValidationGroup="rg" ErrorMessage="Please enter confirm password" 
      ControlToValidate="txtcpwd"></asp:RequiredFieldValidator>
   <br />
   <asp:CompareValidator ID="cmp" runat="server" ForeColor="Red" ValidationGroup="rg"
      ErrorMessage="Password does not match" ControlToCompare="txtpwd"
      ControlToValidate="txtcpwd"  Type="String"
      Operator="Equal"></asp:CompareValidator>
   <asp:Label ID="lblpwd" ForeColor="Red" runat="server" Visible="false"></asp:Label>
</div>
<div class="inp-cont forget-success-hide">
   <label for="inputMob">Mobile No <span class="text-error" style="Color:Red">*</span></label>
   <asp:TextBox runat="server" MaxLength="10" class="signininput"
      onkeypress="inputNumberOnly(event)" ID="txtmobno" placeholder="Enter
      Your Mobile"></asp:TextBox>
   <%--<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
      runat="server" ControlToValidate="txtmobno"
                                                                              ValidationGroup="rg"  ForeColor="Red" ErrorMessage="Enter a valid
      phone number"
                                                                               ValidationExpression="^[7-9][0-9](\s){0,1}(\-){0,1}(\s){0,1}[0-9]{1}[0-9]{7}$"></asp:RegularExpressionValidator><br
      />--%>
   <asp:RegularExpressionValidator ID="RegularExpressionValidator1"
      runat="server" ControlToValidate="txtmobno"
      ValidationGroup="rg"  ForeColor="Red" ErrorMessage="Enter a valid
      mobile number"
      ValidationExpression="^[789]\d{9}$"></asp:RegularExpressionValidator>
   <br
      />
   <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ForeColor="Red"
      ValidationGroup="rg" ErrorMessage="Please enter mobile number" 
      ControlToValidate="txtmobno"></asp:RequiredFieldValidator>
</div>

1 个答案:

答案 0 :(得分:0)

正确使用验证组并在按钮控件中添加验证组并检查验证组名称在按钮控件和其他控件中是否相同也是我们使用asp.net验证控件的地方