Validation controls in ASP.NET 4.0 resolve time consuming and complicated task- verifying user input and reporting errors—and automate it with an elegant, easy-to-use collection of validators. Each validator has its own built-in logic. Some check for missing data, others verify that numbers fall in a predefined range, and so on. In many cases, the validation controls allow you to verify user input without writing a line of code.
ASP.NET provides five validator controls, which are described in next table. Four are targeted at specific types of validation, hile the fifth allows you to apply custom validation routines.
Control class |
Description |
RequiredFieldValidator |
Validation succeeds as long as the input control doesn’t contain an empty string. |
RangeValidator |
Validation succeeds if the input control contains a value within a specific numeric, alphabetic, or date range. |
CompareValidator |
Validation succeeds if the input control contains a value that matches the value in another input control, or a fixed value that you specify. |
RegularExpressionValidator |
Validation succeeds if the value in an input control matches a specified regular expression. |
CustomValidator |
Validation is performed by a user-defined function. |
Each validation control can be bound to a single input control. You can apply more than one validation control to the same input control to provide multiple types of validation. If you use the RangeValidator, CompareValidator, or RegularExpressionValidator, validation will automatically succeed if the input control is empty, because there is no value to validate. If this isn’t the behavior you want, you should also add a RequiredFieldValidator and link it to the same input control. This ensures that two types of validation will be performed, effectively restricting blank values.