Export (0) Print
Expand All

Formatting Validation Error Messages for ASP.NET Server Controls

You can control the format — font, size, and so on — of error text, or you can substitute a glyph for error text. For example, you can set the Text property of a validation control to an asterisk (*). When an error occurs, the asterisk will appear on the page in the location of the validation control (for example, beside the invalid field). You can include a detailed error message in the ErrorMessage property of the validation control and add a ValidationSummary control to the page; the detailed ErrorMessage property text will appear on the page in the location of the ValidationSummary control.

Validation controls support the same properties as other controls for specifying their appearance.

To format error messages

  • Set validation control properties such as the following:
    ForeColorThe color of the error message text.
    BackColorThe color behind the text.
    FontThe font face, size, weight, and so on.
    BorderWidth, BorderColor, and BorderStyleThe size and color of a border around the error message.
    CSSStyle and CSSClassStyle settings that are used if the user is working with a browser that supports CSS style sheets.
    Note   Some of the settings you make with these properties may not be supported in all browsers.

You can provide formatting within an error message by using HTML elements in the text for the ErrorMessage and Text properties. For example, to apply italics to only one word in the error message, you can enclose the word in <I> and </I> tags.

Note   To display reserved HTML characters within the error message text, encode them. For example, to display "<", use the character sequence

If you are displaying validation errors in a summary, you can specify that a glyph appear in place of the individual validation control.

To display glyphs next to input controls

  1. Set the ErrorMessage property of each validation control to the error text that should appear in the ValidationSummary control.
  2. Set the Text property of each validation control to the glyph to display. The glyph can consist of any valid HTML, including text (such as an asterisk) or an image.
    Note   The glyph is displayed in place of the validation control. It is up to you to lay out the validation control in such a way that the glyph displays properly.

See Also

Introduction to Validating User Input in Web Forms | Controlling Validation Error Message Display for ASP.NET Server Controls | Specifying Layout for In-Place Messages On ASP.NET Server Controls

© 2014 Microsoft