asp.net.ph

Skip Navigation Links

Special-Case Validation Results

ASP.NET Web Forms   Web Forms Server Controls   Web Forms Validation


Validation ensures that what the user has entered meets criteria that you specify. In most cases, this is a straightforward operation. However, at times the situations under which input is validated introduce special conditions. It is important that you understand what those conditions are, how validation is performed, and what the results of a validation attempt can be.

Comparing Against Blank Fields

Other than the RequiredFieldValidator control, validation controls consider a field valid if it is blank. For example, if you attach a RangeValidator control to a date field, validation passes either if the user enters a date in the specified range or if the field is left blank. If you want to ensure that the user enters a date, you must add a RequiredFieldValidator control to the control along with the range check.

Comparing Against Another Control

When using the CompareValidator or RangeValidator controls, you can specify that validation be performed against the value of another control. For example, you might have two fields for entering dates, a start date and an end date. Using a RangeValidator control, you can ensure that the minimum value for the end date is the value of the start date.

If the validation controls are unable to resolve the value in the other control, they do not raise an error; instead, the validation check passes. This might occur if:

  • The value in the other control is itself missing or invalid.
  • The validation control cannot find the control to compare against, perhaps because a bad ID for the control was specified.

The specific tests performed by these controls, and possible results, are:

  • If a validation control’s ControlToCompare property is blank, the control sets IsValid to true and validation passes.
  • If the value derived using the ControlToCompare property cannot be converted to the appropriate data type, IsValid is set to false.
  • Finally, if all of these tests have passed, the control performs the actual comparison and sets IsValid appropriately.

This strategy prevents multiple errors from being reported for the same invalid entry. Validation controls are designed to test and report on only one value and to be able to able to display as precise a message as possible when an error is encountered.

For example, in the example of the end date, imagine that the user entered an invalid start date. The user then enters an end date. When the validation check is performed ( usually on the server ), the same bad date could potentially raise an error both for the start date field and for the end date. To prevent this from occuring, the end date validation — the validation dependent on another field — does not raise an error.

You should therefore ensure that you provide sufficient validation for each individual field. In the example, it would be up to you to make sure that the user has entered a proper date in the start date field. In many cases, you should include a RequiredFieldValidator control in addition to a validation control that tests the contents of a specific entry.

See Also

Data Entry Validation ( Client-Side )



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note