Validation Controls

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.

ControlToValidate
The ID of the component for this validator. This property must be set to the ID path of an input component. The ID path is the dot-connected IDs of the components reaching from the validator's parent component to the target component.
ErrorMessage
The text for the error message when the input component failed to validate.
ValidationGroup
If the control causing the validation also sets its ValidationGroup property, only those validators having the same ValidationGroup value will do input validation.
Display
The display behavior of the error message in a validation component. The allowed values are: None, Static and Dynamic. The default is Static.
  • None -- the validator component and the error message will not be displayed.
  • Dynamic -- CSS for the error is constructed in such a way that space for the error message on the page is NOT reserved. When the user hits the "submit" button, applicable error messages will show up shifting the layout of your page around (usually down).
  • Static -- CSS for the error is constructed in such a way that space for the error message on the page is always reserved. When the user hits the "submit" button, applicable error messages will just show up, not altering the layout of your page.
EnableClientScript
Indicating whether client-side validation is enabled. Default is true.

TRequiredFieldValidator

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.

InitialValue
The associated input component fails validation if its value does not change from the InitialValue upon losing focus.

TRegularExpressionValidator

The TRegularExpressionValidator has the following property in addition to the parent TBaseValidator properties.

RegularExpression
The regular expression that determines the pattern used to validate a field. Some commonly used regular expressions include:
  • At least 6 characters: [\w]{6,}
  • Internal URL: http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
  • Japanese Phone Number: (0\d{1,4}-|\(0\d{1,4}\) ?)?\d{1,4}-\d{4}
  • Japanese Postal Code: \d{3}(-(\d{4}|\d{2}))?
  • P.R.C. Phone Number: (\(\d{3}\)|\d{3}-)?\d{8}
  • P.R.C. Postal Code: \d{6}
  • P.R.C. Social Security Number: \d{18}|\d{15}
  • U.S. Phone Number: ((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}
  • U.S. ZIP Code: \d{5}(-\d{4})?
  • U.S. Social Security Number: \d{3}-\d{2}-\d{4}
More regular expression patterns can be found on the Internet, e.g. http://regexlib.com/.

TEmailAddressValidator

TEmailAddressValidator validates whether the value of an associated input component is a valid email address. It will check MX record if checkdnsrr() is available in the installed PHP.

TCompareValidator

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.

ControlToCompare
The input component to compare with the input control being validated.
ValueToCompare
A constant value to compare with the value entered by the user into the input component being validated.
ValueType
The data type (Integer, Double, Currency, Date, String) that the values being compared are converted to before the comparison is made.
Operator
The comparison operation to perform (Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, DataTypeCheck).
DateFormat
The date format to use during comparision.

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.

Integer
A 32-bit signed integer data type.
Double
A double-precision floating point number data type.
Currency
A decimal data type that can contain currency symbols.
Date
A date data type, the date format depends on the DateFormat property.
String
A string data type.

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.

TCustomValidator

TValidationSummary