Validation Group

Ashish
Posted by Ashish under ASP.NET category on | Views : 4857
ValidationGroup
Suppose same page contains login and Registration form having validation.
If you require validation check either on Login form or Signup form for Newslater,at that time ValidationGroup comes in a picture.

Below i have written ValidationGroup example
Here Page contains Two part Login and Signup for Newslater.

txtusername ValidationGroup="Login" Login part
txtPassword ValidationGroup="Login" Login part
txtEmail ValidationGroup="Newslater" Newslater part
btnLogin ValidationGroup="Login" Login part
btnNewslater ValidationGroup="Newslater" Newslater part

Login part Contains :
Two text boxes one for username and other for password both have required validation.On Submit Button(btnlogin) only validation check occurs on Username(txtusername) and password(txtpassword) textbox not on Email textbox(txtEmail).

Signup Newslater part contains:
One textbox for Email(txtEmail) having required and regular validation , On Register button(btnNewslater) only Email validation check not Username nad Password.


txtusername ValidationGroup="Login" Login part
txtPassword ValidationGroup="Login" Login part
txtEmail ValidationGroup="Newslater" Newslater part
btnLogin ValidationGroup="Login" Login part
btnNewslater ValidationGroup="Newslater" Newslater part


Design Part :

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ValidationGroup</title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 181px;
}
.style3
{
width: 154px;
}
.style5
{
width: 182px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table class="style1">
<tr>
<td class="style5">
<b>LOGIN</b></td>
<td class="style3">
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td class="style5">
Username</td>
<td class="style3">
<asp:TextBox ID="txtUsername" runat="server" ValidationGroup="Login"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtUsername" Display="Dynamic" ErrorMessage="Enter Username"
SetFocusOnError="True" ValidationGroup="Login" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style5">
Password</td>
<td class="style3">
<asp:TextBox ID="txtPassword" runat="server" ValidationGroup="Login"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtPassword" Display="Dynamic" ErrorMessage="Enter Password"
SetFocusOnError="True" ValidationGroup="Login" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style5">
&nbsp;</td>
<td class="style3">
<asp:Button ID="btlogin" runat="server" Text="Submit" ValidationGroup="Login" />
</td>
<td>
<asp:ValidationSummary ID="ValidationSummary_Login" runat="server"
ShowMessageBox="True" ShowSummary="False" ValidationGroup="Login" />
</td>
</tr>
<tr>
<td class="style5">
&nbsp;</td>
<td class="style3">
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
</table>

</div>
<table class="style1">
<tr>
<td class="style2">
<b>Signup for Newslater </b>
</td>
<td class="style3">
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td class="style2">
Email Addresss</td>
<td class="style3">
<asp:TextBox ID="txtEmail" runat="server" ValidationGroup="Newslater"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtEmail" Display="Dynamic"
ErrorMessage="Enter email address" ValidationGroup="Newslater" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style2">
&nbsp;</td>
<td class="style3">
&nbsp;</td>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtEmail" ErrorMessage="Enter Valid email address"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ValidationGroup="Newslater" ></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style2">
&nbsp;</td>
<td class="style3">
<asp:Button ID="btNewslater" runat="server" Text="Register"
ValidationGroup="Newslater" />
</td>
<td>
<asp:ValidationSummary ID="ValidationSummary_Newslater" runat="server"
ShowMessageBox="True" ShowSummary="False" ValidationGroup="Newslater" />
</td>
</tr>
<tr>
<td class="style2">
&nbsp;</td>
<td class="style3">
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
</table>
</form>
</body>
</html>

Comments or Responses

Posted by: Neeks on: 2/25/2009 Level:Bronze | Status: [Member]
Really nice article. Thanks, I was looking for it.

Login to post response