In this next example I will briefly explain the use of validators in ASP.Net
When we create web applications, there is always a need to perform client-site validation on user input. .Net really facilitates this with the use of validators. I will show the use of a regularexpression validator.
I have a form that needs some numeric input. It has to be a positive number. When the input is wrong, I don't want my button event to fire. Since I'm using AJAX no postback will be fire anyway, but still the button event is fired. So this is how to correctly handle a situation like this:
First place the input and button on the screen, next to the input you can place the regularexpression validator.
The ASP code looks like this:
<tr>
<td>
<asp:Label ID="Label5" runat="server" Text="Lot number"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtLotNumber" runat="server" ValidationGroup="Numeric">0
</asp:TextBox>
</td>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3"
runat="server"
ControlToValidate="txtLotNumber"
ErrorMessage="Please provide a positive number"
ValidationExpression="^\d+$"
ValidationGroup="Numeric"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td><asp:Button ID="btnSave" Text="Save" runat="server"
OnClick="btnSave_Click"
ValidationGroup="Numeric" Width="100px" /></td>
</tr>
The validationexpression is: ^\d+$ More on these expressions you can find here. As you can see I called the validationgroup numeric. This is because we only have numeric validation on this button event. If you have several validations that need to be performed with this button event, then you'll need a more general validation group name, keep in mind that you can only use 1 validationgroup for the button. When the button is in the same validationgroup as the regularexpressionvalidator, the button event will only be fired when the validations are valid. You can use several validationgroups on one page, in case you have more then 1 event that needs validation. No postback or event are triggered making the user expirience on your site better. No time wasted on refreshing screens or manually perfomring validations. No problems with control states.
After the regular expression was checked, I needed to check on required fields. This is not possible using regular expression validators, so I had to add a required field validator. Using the designer of VS 2008, I added this validator. Then when I tried out the validators, it appeared that the second validator had a space between the control to validate. I googled and found out that you need to set the Display to dynamic. In the following example I changed the first 2 controls, the last I didn't change, you can see the result at the picture below the code:
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Amount"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtAmount" runat="server" ValidationGroup="SaveArrival"></asp:TextBox>
</td>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="txtAmount" ErrorMessage="Please provide a positive number"
ValidationExpression="^\d+$" ValidationGroup="SaveArrival"
Display="Dynamic"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtAmount" ErrorMessage="Amount is required!"
ValidationGroup="SaveArrival" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="K number"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtKNumber" runat="server" ValidationGroup="SaveArrival">0</asp:TextBox>
</td>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtKNumber" ErrorMessage="Please provide a positive number"
ValidationExpression="^\d+$" ValidationGroup="SaveArrival"
Display="Dynamic"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtKNumber" ErrorMessage="K number is required!"
ValidationGroup="SaveArrival" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" runat="server" Text="Lot number"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtLotNumber" runat="server" ValidationGroup="SaveArrival">0</asp:TextBox>
</td>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"
ControlToValidate="txtLotNumber"
ErrorMessage="Please provide a positive number" ValidationExpression="^\d+$"
ValidationGroup="SaveArrival"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtLotNumber" ErrorMessage="Lot number is required!"
ValidationGroup="SaveArrival"></asp:RequiredFieldValidator>
</td>
</tr>
As you can see I used SaveArrival as ValidationGroup, because I now have more then 1 validation type.