Markup Validation in Visual Studio

When you edit markup in the Visual Studio Web designer (for Web Forms pages, in Source view), the editor checks that the markup is valid. Markup validation works like the spelling checker in a word-processing program. The editor examines the markup and adds wavy red lines under the elements or attributes that are not valid.

The editor determines what is valid based on the currently selected validation schema. For example, if you set the schema to HTML5, the editor compares the markup in your page against the schema that defines a valid HTML5 page. If you change the schema to XHTML5, the editor checks the markup in your page against the XHTML5 schema.

A validation error does not prevent the page from running. It simply provides you with feedback that the markup in the page does not conform to the schema for the chosen markup language.

Selecting a Schema

Visual Studio includes schemas for standards such as HTML5, XHTML5, HTML 4.01, and XHTML 1.1. You can let Visual Studio determine the schema based on the DOCTYPE value in the page. Alternatively, you can select a schema from the drop-down list in the HTML Source Editing toolbar. You can also select a default validation schema in the Options dialog box.

Viewing Validation Errors

The editor shows you validation errors in the following ways:

  • Underlined elements   Elements or attributes that are invalid are underlined with wavy lines.

  • Tooltips   If you hold the mouse pointer over an element underlined with a wavy line, a tooltip displays details about the error.

  • Error List   If you have the Error List window open, you can see a list of all the validation errors (as warnings). To open the Error List window, choose View, and then choose Error List. To open the list from the keyboard, type Ctrl+\ and then type E. Double-clicking an error in the Error List window takes you directly to the error in the editor.

What Validation Checks

Markup validation checks for the following:

  • Allowed tags   Some browser schemas support tags that are not allowed in others. For example, the <blink> tag is supported only in Netscape browsers, so this tag is marked as invalid in other schemas.

  • Allowed attributes   Validation checks that the attributes in an element are allowed for that element.

  • Required attributes   Validation checks that any required attributes are included. For example, in XHTML 1.1, the <script> tag must have a type attribute.

  • Allowed attribute values   If an attribute supports only certain enumerated values, such as true or false, validation marks any values that do not conform to the allowed values.

  • Allowed CSS values   Validation checks that the cascading style sheet (CSS) values for the style attribute are allowed.

  • Allowed child elements   Validation checks that child elements are allowed for a given parent element.

  • Proper use of quotation marks around attribute values   Depending on the schema, quotation marks around attribute values might be required. For schemas where quotation marks are not required, validation checks that the quotation marks match if they are present. When quotation marks around attribute values are not required by the schema, you can configure whether Visual Studio checks them. See "Customizing Validation" later in this topic.

  • Casing rules   Depending on the schema, element and attribute names might need to be in lowercase letters. If the schema allows uppercase or lowercase letters in names, validation checks them according to the validation options you set. See "Customizing Validation" later in this topic.

  • Unique element IDs Validation ensures that element IDs are unique throughout the page.

  • Well-formed elements   Validation confirms that all opening tags have corresponding closing tags, if the schema requires it.

  • Element opening and closing order   Validation checks that you do not have crossed opening and closing elements, such as <b><i>text</b></i>.

  • Outdated tags or attributes   A schema such as XHTML5 marks tags such as <font> with an error that tells you that the tag is no longer supported.

  • Valid targets for relative hyperlinks and graphics   Validation confirms the validity of any targets that are relative to the current site, but it does not check the validity of fully qualified URLs.

Validating Page Output

Validation checks the markup of the page in the designer. It cannot guarantee that when the page runs in the browser, the output of the page will validate against a specific schema. For Web Forms pages, individual ASP.NET server controls and the page itself render markup, and sometimes script, into the page when the page is processed. That output is not accessible to validation in the designer. The markup and scripts that are created at run time are valid for most browsers. ASP.NET pages and server controls render markup that is XHTML5-compatible. For more information, see XHTML Standards in Visual Studio and ASP.NET.

Validation and IntelliSense

Specifying a schema controls not just validation, but the list of tags and attributes that are available in IntelliSense. For example, the XHTML5 schema does not offer the <font> tag because <font> tags are deprecated in that schema.

Customizing Validation

Depending on the schema that you are using, you might be able to specify additional validation options. For example, if you are using HTML5, you can select whether the following conditions show as errors:

  • when client tags are not lowercase

  • when tags are not well-formed

  • when attribute values are not quoted

  • when attribute or element is deprecated

Validating Multiple Namespaces

In a web page, it is possible to include blocks of markup that are declared with their own namespace. For example, a block of embedded XML in a page might contain a namespace declaration for its own elements. In that case, validation is based on both the current page schema and on the schema for the embedded block.

See Also

Other Resources

XHTML Standards in Visual Studio and ASP.NET