DataTypeCheck The RangeValidator Control The RangeValidator server control is similar to the CompareValidator server control, but the RangeValidator server control compares what is entered into the form field with two values and makes sure that what was entered by the user is between these two specified values.
For instance, imagine that you have a text box where you want end users to enter their ages. Instead of being greater than or less than a specific constant, you want the values entered to be between a specific range of numbers.
For this, you use the RangeValidator server control, as illustrated in Listing 9. Using the RangeValidator server control to work with a range of numbers Age: If some number is entered that is outside of this range, the RangeValidator server control fires an error message and considers the form submission invalid. The Type property enables you to make comparisons against many different.
These choices enable you to do a number of range comparisons. For instance, you can use the Currency value in the Type property to retrieve monetary-value entries that are within a certain range. You can also use the Date value for the Type property to make sure that the entry is between specific date ranges. Also, just as you can use the String data type in the CompareValidator server control, you can use the String data type with the RangeValidator server control to make sure that the value entered falls within a specific range of characters.
For example, if the user is entering her last name, and you want only people with last names starting with M and P to proceed, you can easily do this by using the RangeValidator server control, as illustrated in Listing Comparing an entry to a range of characters Last name: Notice, in this example, that the Type property is not specified. In this case, it doesn't need to be specified because the default value of the Type property is String. If not specified, it is considered to have the value of String.
The RegularExpressionValidator Control The RegularExpressionValidator server control is a validation control that enables you to check the user's input based on a pattern defined by a regular expression.
NET saves coding time. NET provides you with a short list of expressions to use in your form via the Regular Expression Editor. However, you are not limited to these regular expressions in your ASP. The list of prepared expressions is shown in Figure 5. The Regular Expression Editor For an example of using the RegularExpressionValidator server control to make sure that a value entered in a text box is an e-mail address, look at Listing Validating an e-mail address Email: The great thing is that it is pretty simple, and it takes hardly any coding.
Figure 6 shows the error message that results if a user enters an incorrect e-mail address in the text box. The e-mail address is not checked in to ensure that it is an actual e-mail address. Using Images for Your Error Messages One interesting way of showing your error messages when using validation controls is to use images along with text for identifying errors on your ASP.
This secret is not limited to the RegularExpressionValidator server control, but can be used with all the validation server controls. To use an image instead of text for your error messages, you create something similar to the code in Listing Using images for your validation messages Email: An image is displayed when the incorrect e-mail address is entered. The CustomValidator Control You are not limited to the validation controls that have been shown thus far in this article; you also have the CustomValidator server control.
The CustomValidator server control enables you to develop your own custom server-side or client-side validations. At times, you may want to compare the user's input to a value in a database, or to determine whether his input conforms to some arithmetic validation that you are looking for for instance, if the number is even or odd. You can do all this and more by using this type of validation control.
Performing a custom client-side validation using the CustomValidator server control You set the args. Value property is the value from the user that is retrieved from the control that the CustomValidator server control is tied to. NET controls so that they behave like the other validation controls. Server-side Validation The other way of performing validation on Web forms using the CustomValidator server control is to use server-side validation.
This is just as easy as the client-side validation. Server-side validation of your Web forms enables you to create rather elaborate validation capabilities. Listing 15 shows you a not-too-elaborate example of server-side checking. Here the code determines whether the number entered in the text box on the ASP. NET page is even. Creating your own server-side validation functions Visual Basic. The ClientValidationFunction is used with the CustomValidator server control when working with client-side validation.
In this case, you need to give the OnServerValidate property a value that is equal to the name of the server-side function that you would write in one of the.
Making Validation More Secure If you are going to use the CustomValidator server control for client-side validation, you should also consider re-evaluating the user's input using a server-side validation function. It is not too hard for some people to post a form back to your server and bypass or fool the client-side validation. If you re-evaluate the input on the server, you can stop this from occurring, making your ASP.
NET applications more secure. Using the CustomValidator server control to validate the Checkbox server control If you have been working through the examples so far in this article, note that there wasn't a validation server control in place that was able to validate the CheckBox server control. Don't be too worried. You can use the CustomValidator server control to work through this control. You can use it any time that a validation server control on your page is not using the ControlToValidate property.
For an example of this, see Listing Validating a check box Visual Basic. Also notice that there isn't a validation server control on the page that has this CheckBox server control tied to it via any property settings.
Within this server-side function, validation performed in the code checks whether the CheckBox server control's Checked property has a value of True meaning that it is checked. If the value of this property is True, the CustomValidator server control is passed a True value—meaning that the input passed the test.
The CustomValidator server control enables you to do almost any type of validations that you can think of. This is a great control to use if you do any database validations on the input that is entered into a form by a user.
This control can also apply any complicated logic that you want to include in the validation process. The ValidationSummary Control The ValidationSummary server control works with all the validation server controls on the page. It takes all the error messages that the other validation controls send back to the page and puts them all in one spot that you specify on the page.
These error messages can be displayed in a list, bulleted list, or paragraph. Showing a Bulleted List of Errors You can use the ValidationSummary server control in a number of ways, but the example in Listing 17 shows you how to use it in a simple manner.
For this example, two text boxes on the page are associated with a RequiredFieldValidator control. When an error is triggered, not only does it display the error next to the text box itself, it also displays it in a summary at the bottom of the ASP. Validation errors shown using the ValidationSummary server control By default, the ValidationSummary server control shows the errors in a bulleted list on the page using red text.
You have the option to completely alter how output displays in the browser. To change how the error messages are displayed, you can change the value of the DisplayMode property of the ValidationSummary control.
The possible values of this control can be set to the following: If you use List, it appears without bullets. If you use SingleParagraph, the errors appear in a text area all on one line in a single paragraph. NET page's errors in a pop-up dialog box. You have the option of showing the summary in the browser and the dialog box together, or just in the dialog box. The property that controls whether the message appears in the browser is the ShowSummary property. To turn off the display of validation errors in the browser, set the value of the ShowSummary property to False.
A dialog box showing the page's validation errors Understanding the Difference Between the ErrorMessage and Text Properties In the examples shown so far using the ValidationSummary server control, the error messages were next to the items that were being validated the RequiredFieldValidator server controls and were displayed within the ValidationSummary server control.
To accomplish this, you use the specific validation controls—not the ValidationSummary server control itself. For instance, if there is a RequiredFieldValidator server control validating a text box, you construct this validation control as shown in Listing Using different text for the validation error messages When both the ErrorMessage and Text properties are used for the validation controls, the value of the ErrorMessage property is displayed in the ValidationSummary server control's listing of validation errors, and the value assigned to the Text property is displayed in the validation control itself.
Validation and Web controls make an outstanding combination for building smart forms. About the Author Bill Evjen is an active proponent of.
NET technologies and community-based learning initiatives for.