Partager via


How to: Format Validation Error Messages for ASP.NET Server Controls

When using ASP.NET validation controls, you can control the format—font, size, and so on—of error text, or you can substitute a marker for error text. For example, you can have the validation control display an asterisk (*) when an error occurs.

Note

Using a marker in place of text can detract from the accessibility of the page. For more information, see ASP.NET Accessibility.

You can also 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.

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 &lt;.

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

To format error messages

  • Set validation control properties such as the following:

    Property

    Description

    ForeColor

    The color of the error message text.

    BackColor

    The color behind the text.

    Font

    The font face, size, weight, and so on.

    BorderWidth, BorderColor, and BorderStyle

    The size and color of a border around the error message.

    Style and CssClass

    Style 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.

To display markers 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 marker to display. The marker can consist of any valid HTML, including text (such as an asterisk) or an image.

    Note

    The marker 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 marker displays properly.

See Also

Tasks

How to: Control Validation Error Message Display for ASP.NET Server Controls

How to: Specify Layout for In-Place Messages On ASP.NET Server Controls

Concepts

Types of Validation for ASP.NET Server Controls

Other Resources

ASP.NET Validation Controls