Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [207]

By Root 5428 0
runat="server" id="emailValidator"

ControlToValidate="email"

ValidationExpression="[a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+"

ErrorMessage="Must be an email address." />

Hire Date

ControlToValidate="hired"

Display="Static"

Operator="DataTypeCheck"

Type="date"

ErrorMessage="Must enter a date." />

ControlToValidate="hired"

Display="Dynamic"

MinimumValue="1999-1-1"

MaximumValue="9999-12-31"

Type="Date"

ErrorMessage="Date after 1-1-99." />

Membership Level

ControlToValidate="membership"

ClientValidationFunction="CheckMembership"

ErrorMessage="Must be Gold or Platinum." />

The hired control is being validated by a CompareValidator and a RangeValidator at the same time. Validation takes place in order, and each validation control generates and displays its own error message. The content of the input control is considered valid if all the validators return true. If an input control has multiple valid patterns—for example, an ID field can take the form of a Social Security number or a VAT number—you can either validate by using custom code or regular expressions.

Note

The preceding HTML snippet uses a table element to lay out the input fields around the form. This approach is discouraged and plain block elements should be used (DIV and P tags) that could be lined up via CSS styles. Unfortunately, I’m not a CSS expert.

The ValidationSummary Control


The ValidationSummary control is a label that summarizes and displays all the validation error messages found on a Web page after a postback. The summary is displayed in a single location formatted in a variety of ways. The DisplayMode property sets the output format, which can be a list, bulleted list, or plain text paragraph. By default, it is a bulleted list. The feasible values are grouped in the ValidationSummaryDisplayMode enumeration.

Whatever the format is, the summary can be displayed as text in the page, in a message box, or in both. The Boolean properties ShowSummary and ShowMessageBox let you decide. The output of the ValidationSummary control is not displayed until the page posts back no matter what the value of the EnableClientScript property is. The HeaderText property defines the text that is displayed atop the summary:

ShowMessageBox="true"

ShowSummary="true"

HeaderText="The following errors occurred:"

DisplayMode="BulletList" />

This code snippet originates the screen shown in Figure 9-6.

Figure 9-6. After the page posts back, the validation summary is updated and a message box pops up to inform the user of the errors.

The validation summary is displayed only if there’s at least one pending error. Notice that, in the default case, the labels near the input controls are updated anyway, along with the summary text. In summary, you can control the error information in the following ways:

Both in-place and summary information This is the default scenario. Use the ValidationSummary control, and accept all default settings on the validator controls. If you want to leverage both places to display information, a recommended approach consists of minimizing the in-place information by using the Text property rather than ErrorMessage. If you set both, Text is displayed in-place while ErrorMessage shows up in the validation summary. For example, you can set Text with a glyph or an exclamation mark and assign ErrorMessage with more detailed text.

Only in-place information Do not use the ValidationSummary control, and set the ErrorMessage property in each validation control you use. The messages appear after the page posts back.

Only summary information Use the ValidationSummary control, and set the ErrorMessage property on individual

Return Main Page Previous Page Next Page

®Online Book Reader