ASP.NET Validation Controls


ASP.NET offers more validation controls than the one we have already talked about, the RequiredFieldValidator . It also offers the ValidationSummary , the CompareValidator , the RangeValidator , the RegularExpressionValidator , and the CustomValidator controls. The ValidationSummary control is used to display a summary of all the ErrorMessages on a given form. So, if you want to somehow summarize for the user the data validation errors that have occurred in a form, you could use the ValidationSummary control. The CompareValidator control is used to compare the value of a field to another field. The RangeValidator is used to verify that the field value falls within a given range. For example, if you are asking a user for some sort of number that falls between the value of 5 and 25, it verifies that the value the user types is truly in this range.

The RegularExpressionValidator might be the most powerful of all validation controls. It can use regular expressions to verify the contents of a field. And finally, the CustomValidator control is used to build your own validation algorithm, in case even regular expressions are not enough.

The ValidationSummary Control

The next thing to do is to take a look at an example that has three required fields. I will place a ValidationSummary control at the bottom of the form. Not only do the required fields come up and indicate where there is an error, but the ValidationSummary control displays all the errors in one convenient location. Take a look at the application I have created in Figure 12.5. It has three editable fields, RequiredFieldValidator controls for each one, and a ValidationSummary control at the bottom.

Figure 12.5. This application has a ValidationSummary to give users an easy way to review errors.


When this application runs, it validates each field with the RequiredFieldValidators to ensure that all fields have some sort of data in them. If any of the fields have no data, the RequiredFieldValidators lets the user know this. At the same time, at the bottom of the form, the ValidationSummary control displays a list of all the errors, as shown in Figure 12.6.

Figure 12.6. This form has two fields that were not filled in.


The CompareValidator Control

The CompareValidator control is very easy to use. As with the RequiredFieldValidator control, you must tell it which control you want it to validate. This is done by setting the ControlToValidate property with the ID of the control that you want to validate. And as with the RequiredFieldValidator , you can set any message in the ErrorMessage property. The three additional properties that you probably will use most often are going to be the operator property, the type property, and the value to compare property.

The operator property contains the comparison operator that you will use. Your choices are equal, not equal, greater than, greater than equal, less than, less than equal, and data type check. The type property determines what type of data you are expecting in the control. Your choices are string, integer, double, date, and currency. The third property is the value to compare property. This contains the value to which you want to compare the control.

The following code is a simple demonstration application with three fields in it. One asks for the user's age, another asks for the user's annual income, and the last asks for the user's weight. For each of these editable fields I have added a CompareValidator control. You can see the application as I have developed it in in Figure 12.7.

Figure 12.7. This simple application accepts three values and humorously compares them to set values.


 <body>    <form id="Form1" method="post" runat="server">      <P><FONT size="5">Please enter the following information:</FONT>      </P>      <P>        <asp:Label id="Label2" runat="server">Your Age: </asp:Label>      <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>        <asp:CompareValidator id="CompareValidator1" runat="server"           ErrorMessage="Aren't you too young to be programming ASP.NET?"          ControlToValidate="TextBox1" Operator="GreaterThanEqual"           ValueToCompare="10" Type="Integer"></asp:CompareValidator></P>      <P>        <asp:Label id="Label3" runat="server">Your Annual Income: </asp:Label>        <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>        <asp:CompareValidator id="CompareValidator2" runat="server"           ErrorMessage="At that income I would hire a developer to read this book!"          ControlToValidate="TextBox2" Operator="LessThanEqual" ValueToCompare="200000"         Type="Currency"></asp:CompareValidator>      </P>      <P>        <asp:Label id="Label4" runat="server">Your Weight: </asp:Label>        <asp:TextBox id="TextBox3" runat="server"></asp:TextBox>        <asp:CompareValidator id="CompareValidator3" runat="server"          ErrorMessage="You should be exercising instead of programming."         ControlToValidate="TextBox3" Operator="LessThanEqual" ValueToCompare="600"        Type="Integer"></asp:CompareValidator>      </P>      <P>        <asp:Button id="Button1" runat="server" Text="Submit"></asp:Button></P>    </form>  </body> 

When this application runs, users might or might not be notified by the CompareValidator controls. For example, if users type any age that is 10 or less they get a message; otherwise , none appears because the CompareValidator has decided that the age that they entered was in range. In this way, you can alert users to values that are way off, or say nothing when the values are acceptable.

The RangeValidator Control

The RangeValidator control is similar to the CompareValidator control. But whereas the CompareValidator control uses a single value to do its comparison, the RangeValidator uses a minimum and a maximum value to validate its data. The RangeValidator does not offer an Operation property as does the CompareValidator control. It just determines that the data that the user has entered is within its expected range of values. This control offers the same data types as the CompareValidator control: string, integer, double, date, and currency.

The RegularExpressionValidator Control

The RegularExpressionValidator control gives you even greater flexibility than the validation control that has already been covered. With it, you can create any valid regular expression, put the regular expression in the validation expression property, and the RegularExpressionValidator control does the rest. It is a great way to evaluate e- mails , addresses, phone numbers , ZIP codes, and many other things that users are often asked to enter. And what is even better is that many of the regular expressions for common items can be selected in a list box and are then automatically inserted into the validation expression property. You can see in Figure 12.8 where I am selecting a U.S. phone number. If users type a valid U.S. phone number, no error message appears. However, if the user types an invalid U.S. phone number, an error message does appear.

Figure 12.8. Many common validation expressions are available through this selector.


 Phone Number:      <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>      <asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server"        ErrorMessage="The phone number you entered is invalid" ControlToValidate="TextBox1"        ValidationExpression="((\(\d{3}\) ?)(\d{3}-))?\d{3}-\d{4}">        </asp:RegularExpressionValidator> 

Of course, there will be times when these built-in regular expressions will not be adequate for your purposes. If this is the case, you will have to drop down and write your own regular expression for the control. The "Regular Expressions Primer" section that follows includes a section of regular expressions for your reference.


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233 © 2008-2017.
If you may any questions please contact us: