Validation controls perform validation on both the client (the browser) and the server. The validation controls use client-side JavaScript. This is great from a user experience perspective because you get immediate feedback whenever you enter an invalid value into a form field.
Even when validation happens on the client, validation is still performed on the server. This is done for security reasons. If someone creates a fake form and submits the form data to your web server, the person still won't be able to submit invalid data.
You can disable client-side validation for any of the validation controls by assigning the value False to the validation control's EnableClientScript property.
Always check the Page.IsValid property when working with data submitted with a form that contains validation controls. Each of the validation controls includes an IsValid property that returns the value true when there is not a validation error. The Page.IsValid property returns the value true when the IsValid property for all of the validation controls in a page returns the val True.
The Controls are
· RequiredFieldValidator Enables you to require a user to enter a value in a form field.
· RangeValidator Enables you to check whether a value falls between a certain minimum and maximum value.
· CompareValidator Enables you to compare a value against another value or perform a data type check.
· RegularExpressionValidator Enables you to compare a value against a regular expression.
· CustomValidator Enables you to perform custom validation.
· ValidationSummary Enables you to display a summary of all validation errors in a page.
The validation controls can be associated with any of the form controls included in the ASP.NET Framework.
RequiredFieldValidator Control
The RequiredFieldValidator control enables you to require a user to enter a value into a form field before submitting the form. You must set two important properties when using the RequiredFieldValdiator control:
ControlToValidate: The ID of the form field being validated.
Text: The error message displayed when validation fails.
RangeValidator Control
The RangeValidator control enables you to check whether the value of a form field falls between a certain minimum and maximum value. You must set five properties when using this control:
ControlToValidate - The ID of the form field being validated.
Text - The error message displayed when validation fails.
MinimumValue - The minimum value of the validation range.
MaximumValue - The maximum value of the validation range.
Type -The type of comparison to perform. Possible values are String, Integer, Double, Date, and Currency.
CompareValidator Control
The CompareValidator has six important properties:
ControlToValidate - The ID of the form field being validated.
Text -The error message displayed when validation fails.
Type - The type of value being compared. Possible values are String, Integer, Double, Date, and Currency.
Operator -The type of comparison to perform. Possible values are DataTypeCheck, Equal, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, and NotEqual.
ValueToCompare -The fixed value against which to compare.
ControlToCompare -The ID of a control against which to compare.
RegularExpressionValidator Control
The regular expression is assigned to the RegularExpressionValidator control's ValidationExpression property. It looks like this:
\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
CustomValidator Control
The CustomValidator control has three important properties:
ControlToValidate - The ID of the form field being validated.
Text - The error message displayed when validation fails.
ClientValidationFunction - The name of a client-side function used to perform client-side validation.
The CustomValidator also supports one event:
ServerValidate - This event is raised when the CustomValidator performs validation.
ValidationSummary Control
The ValidationSummary control enables you to display a list of all the validation errors in a page in one location. If you use the ValidationSummary control, however, you can always display a list of errors at the top of the form.
Creating Custom Validating Controls
You create a new validation control by deriving a new control from the BaseValidator class. As its name implies, the BaseValidator class is the base class for all the validation controls.
The BaseValidator class is a MustInherit (abstract) class, which requires you to implement a single method:
EvaluateIsValid Returns true when the form field being validated is valid.
The BaseValidator class also includes several other methods that you can override or otherwise use. The most useful of these methods is the following:
GetControlValidationValue Enables you to retrieve the value of the control being validated.
When you create a custom validation control, you override the EvaluateIsValid() method and, within the EvaluateIsValid() method, you call GetControlValidationValue to get the value of the form field being validated.
No comments:
Post a Comment