I’ve been reading an ASP.Net book lately and just got to the information about Validation controls. These seem like a pretty sweet way to get a lot done with very little code. For example, say a that I want a text field that is required, all I have to do is add:
<asp:RequiredFieldValidator ID="reqNum" runat="server"
ControlToValidate="txtUser" Text="Number is required"/>
This will perform a validation on a control named “txtUser” making sure that it has a value. This is all I have to do to get both client-side Javascript validation and server-side validation. Both of these validations will fire, so if the user has Javascript disabled in their browser, the server-side validation will still happen. However, be fore warned that the pages is posted and the click event will fire, regardless of whether or not the input is valid. There is a property of the Page class called IsValid that is set to false if a validation control has failed. This means that your button’s click function should look something like this:
if (Page.IsValid)
{ doStuff(); }
The RequiredFieldValidator also has an initial value as a property, and will fail if the field is still that initial value. This is especially useful with Drop Downs that you want to make sure that the user has selected a value in:
Region: <asp:DropDownList ID="lstRegion" runat="server">
<asp:ListItem Value="0" Selected="True">Choose a region
</asp:ListItem>
<asp:ListItem Value="1">East</asp:ListItem>
<asp:ListItem Value="2">West</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="reqRegion" runat="server"
ControlToValidate="lstRegion" Text="Choose a region" InitialValue="0"/>
Now if the user does not change the value of the Drop Down, the validation will fail.
There are three other built in validators and you can create custom validators. The custom validators can have a client-side validation function written in JavaScript or VBScript, but they should always have a server-side function tied to OnServerValidate in case the user has scripts turned off in the browser. There is a lot to be said about validation controls, but I’d like to mention a couple other things I found interesting.
You can use one CompareValidator to validate the value and type of a field. Setting the Operator to something besides “DataTypeCheck” and setting the Type property will both make sure the value fulfills the comparison and is of the right type. For example, if you wanted the user to input an integer greater than 0, you could use the following validation control:
<asp:CompareValidator ID="compNumber" runat="server"
ControlToValidate="txtNumber" Text="Must be an integer greater than 0"
Type="Integer" Operator="GreaterThan" ValueToCompare="0"/>
Another thing to note is that by default these validation controls will take up the space on the page required by the value of the Text property. Setting the Display property to “Dynamic” will make them only take up space if the message is visible. If you plan on having multiple validations on the same control, you should make the Display=”Dynamic” so that you don’t end up with strange looking blank spots.
Here’s the plug: I’ve been reading “Core Internet Application Development with ASP.Net 2.0” by Randy Connolly. I’ve found it quite good so far.
