Understanding ASP.Net Validation Controls - The Syntax Of Validation Controls
(Page 2 of 4 )
The syntax of these validation controls is very simple and you don't have to deal with complicated JavaScript validation code. All of the Validation controls inherit from the Label control.
Here are some of their most important properties:
- ControlToValidate: The ID of the control that this validator validates.
- Display: Has three possible values: Dynamic, Static and None. Dynamic means that the space the control uses isn’t reserved for the control. Static means that the space it uses is always reserved for the control, and lastly None makes the control invisible.
- EnableClientScript: This means that the validation occurs on the server (the default is true).
- Text: Is displayed when the validation fails; it’s often used to put an asterisk or an icon next to the error and for displaying the error message in a validation summary.
Remember that if you have VS.NET you don't need to hard code all of these properties; just open the properties dialog in Visual Studio using the design mode.
The simplest control is the RequiredValidator Control, which as the name implies, looks for the specified control to validate and checks if it contains a specific value.
It compares the value of a control with its InitialValue property (which defaults to an empty string). If the two values are the same then it evaluates to false. If the InitialValue property isn't an empty string, a blank entry would be valid, so the simplest way to avoid this is with the use of two controls.
Here's an example of using the control with a Textbox:
Email: <asp: Textbox runat="Server" ID="email" />
<asp:RequiredFieldValidator runat="Server" ControlToValidate="email" ErrorMessage="Please enter an email address" ID="emailReqValidator" />The CompareValidator ControlThe next control is CompareValidator, which you use to compare a control's value with a static value or another control's value. This control exposes four properties to define how the comparison is performed:
- Type: Describes the type of data expected in the ControlToValidate.
- Operator: Determines the type of operation the control is going to perform. A very important option is DataTypeCheck, which will be used to check if the value in the Type property is correct.
- ControlToCompare: The ID of the control to compare with.
- ValueToCompare: The static value to compare with the ControlToValidate.
Remember that only one instance of the last two properties can be given to a single control.
Here's an example that uses the CompareValidator control. We are using it to make sure the first textbox has an integer value, and then we use it to ensure that the first textbox value is greater than the second.
Here's the code:
<asp:TextBox id="Num1" runat="server"></asp:TextBox>
<asp:TextBox id="Num2" runat="server"></asp:TextBox>
<asp:CompareValidator id="CompareValidator1" runat="server" ErrorMessage="You have to write an integer value" ControlToValidate="Num1" Type="Integer" Operator="DataTypeCheck"></asp:CompareValidator>
<asp:CompareValidator id="CompareValidator2" runat="server" ErrorMessage="The 1st value have to be greater than the 2nd" ControlToValidate="Num2" Type="Integer" ControlToCompare="Num1" Operator="LessThan"></asp:CompareValidator>

The next control is RangeValidator, which is a little simpler than the CompareValidator control. This control simply checks that the value falls into a specific range.
This range is determined from the MaximumValue and MinimumValue properties; this control also contains the Type property, just like the CompareValidator control.
Here's an example of using the RangeValidator control to ensure that the value in the textbox falls between 100 and 300:
<asp:textbox id="weight" runat="server"></asp:textbox>
<asp:rangevalidator id="RangeValidator1" runat="server" ErrorMessage="Write a correct weight" ControlToValidate="weight" Type="Integer" MaximumValue="300" MinimumValue="100"></asp:rangevalidator>
Next: The RegularExpressionValidator Control >>
More ASP.NET Articles
More By Armando Andrade