Validation is performed when a postback control, such as a TButton, a TLinkButton or a TTextBox (under AutoPostBack mode) is submitting the page and its CausesValidation property is true.
Validator controls always validate the associated input control on the serve side. In addition, if EnableClientScript is true, validation will also be performed on the client-side using javascript. Client-side validation will validate user input before it is sent to the server. The form data will not be submitted if any error is detected. This avoids the round-trip of information necessary for server-side validation.
Every validator component has the following properties, defined in the TBaseValidator class.
This is the simplest validator, ensuring that the input field has some sort of value. To ensure that all of our input fields are required, add a TRequiredFieldValidator component for each of the input fields. The TRequiredFieldValidator also has the following property.
The TRegularExpressionValidator has the following property in addition to the parent TBaseValidator properties.
[\w]{6,}
(0\d{1,4}-|\(0\d{1,4}\) ?)?\d{1,4}-\d{4}
\d{3}(-(\d{4}|\d{2}))?
(\(\d{3}\)|\d{3}-)?\d{8}
\d{6}
\d{18}|\d{15}
((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}
\d{5}(-\d{4})?
\d{3}-\d{2}-\d{4}
Note, if the value being validated is empty, TRegularExpressionValidator will not do validation. Use a TRequiredFieldValidator to ensure the value is not empty.
TEmailAddressValidator validates whether the value of an associated input component is a valid email address. If CheckMXRecord is true, besides checking the format of the input value, the validator will also check MX record for the email address, provided checkdnsrr() is available in the installed PHP.
Note, if the value being validated is empty, TEmailAddressValidator will not do validation. Use a TRequiredFieldValidator to ensure the value is not empty.
The validator TCompareValidator is used to compare two input fields, the comparison can be made in many ways. The following are the properties of the TCompareValidator in addition to the parent TBaseValidator.
To specify the input component to validate, set the ControlToValidate property to the ID of the input component. To compare the associated input component with another input component, set the ControlToCompare property to the ID of the component to compare with.
To compare the associated input component with a constant value, specify the constant value to compare with by setting the ValueToCompare property.
The ValueType property is used to specify the data type of both comparison values. Both values are automatically converted to this data type before the comparison operation is performed. The following value types are supported.
Use the Operator property to specify the type of comparison to perform. If you set the Operator property to DataTypeCheck, the TCompareValidator component will ignore the ControlToCompare and ValueToCompare properties and simply indicates whether the value entered into the input component can be converted to the data type specified by the ValueType property.
Note that if the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data into the input control.