Data Validation

In addition to providing controls for data binding, Atlas ships with its own controls for validating data the user enters, a feature that many ASP.NET developers find useful. Atlas supports the following validators:

requiredFieldValidator

Checks whether the user has entered a value into a control

regexValidator

Checks the data in a control against a regular expression to match a pattern

typeValidator

Checks the data in a control against a data type

rangeValidator

Checks the data in a control against a value range

customValidator

Checks the data in a control using a custom validation function

To implement data validation, you need:

  • A control to validate

  • A way to display an error message if the validation fails

  • Code or markup to do the validation

In the following sections you’ll see how to put each of the Atlas validators to work, including how to do your own custom validation.

Checking a Required Field

The requiredFieldValidator class, which checks whether a control contains data, is a commonly used Atlas data validator. The following markup generates both an input field and a span to display any error messages that the validator generates:

<input type="text" id="TextBox1" />
<span id="Error1" style="color: red;">*</span>

As you can see, the label for the error message is not hidden by default. Atlas takes care of hiding it automatically.

In the xml-script for the page, add markup for the HTML elements taking part in the validation—just the elements, not any controls ...

Get Programming Atlas now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.