In this article i explain you how to use validation controls in registration form
<form id="form1" runat="server">
<div style="margin-top:50px;border:1px solid black;width:500px;padding:10px;margin-left:50px;">
<table>
<tr>
<td>First Name</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RfvFirstName" runat="server" ControlToValidate="txtFirstName" ForeColor="Red" ErrorMessage="Enter FirstName" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Last Name</td>
<td>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RfvLastName" runat="server" ControlToValidate="txtLastName" ForeColor="Red" ErrorMessage="Enter LastName" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Gender</td>
<td>
<asp:DropDownList ID="ddlGender" runat="server" Height="25px" Width="170px">
<asp:ListItem Text="Select Gender" Value="-1"></asp:ListItem>
<asp:ListItem Text="Male" Value="1"></asp:ListItem>
<asp:ListItem Text="Female" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RfvGender" runat="server" ControlToValidate="ddlGender" ForeColor="Red" InitialValue="-1" ErrorMessage="Select Gender" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Email</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RfvEmail" runat="server" ControlToValidate="txtEmail" ForeColor="Red" ErrorMessage="Enter Email" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Password</td>
<td>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RfvPassword" runat="server" ControlToValidate="txtPassword" ForeColor="Red" ErrorMessage="Enter Password" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Confirm Password</td>
<td>
<asp:TextBox ID="txtConfirmPassword" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RfvConfirmPassword" runat="server" ControlToValidate="txtConfirmPassword" ForeColor="Red" ErrorMessage="Enter Confirm Password" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="Cmp1" runat="server" ControlToValidate="txtConfirmPassword" ControlToCompare="txtPassword" ForeColor="Red" Display="Dynamic" SetFocusOnError="true" ErrorMessage="Plese Enter Same Password"></asp:CompareValidator>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</td>
</tr>
</table>
</div>
</form>
Output:-
<form id="form1" runat="server">
<div style="margin-top:50px;border:1px solid black;width:500px;padding:10px;margin-left:50px;">
<table>
<tr>
<td>First Name</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RfvFirstName" runat="server" ControlToValidate="txtFirstName" ForeColor="Red" ErrorMessage="Enter FirstName" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Last Name</td>
<td>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RfvLastName" runat="server" ControlToValidate="txtLastName" ForeColor="Red" ErrorMessage="Enter LastName" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Gender</td>
<td>
<asp:DropDownList ID="ddlGender" runat="server" Height="25px" Width="170px">
<asp:ListItem Text="Select Gender" Value="-1"></asp:ListItem>
<asp:ListItem Text="Male" Value="1"></asp:ListItem>
<asp:ListItem Text="Female" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RfvGender" runat="server" ControlToValidate="ddlGender" ForeColor="Red" InitialValue="-1" ErrorMessage="Select Gender" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Email</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RfvEmail" runat="server" ControlToValidate="txtEmail" ForeColor="Red" ErrorMessage="Enter Email" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Password</td>
<td>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RfvPassword" runat="server" ControlToValidate="txtPassword" ForeColor="Red" ErrorMessage="Enter Password" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Confirm Password</td>
<td>
<asp:TextBox ID="txtConfirmPassword" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RfvConfirmPassword" runat="server" ControlToValidate="txtConfirmPassword" ForeColor="Red" ErrorMessage="Enter Confirm Password" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="Cmp1" runat="server" ControlToValidate="txtConfirmPassword" ControlToCompare="txtPassword" ForeColor="Red" Display="Dynamic" SetFocusOnError="true" ErrorMessage="Plese Enter Same Password"></asp:CompareValidator>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</td>
</tr>
</table>
</div>
</form>
Output:-
No comments:
Post a Comment