Validating Your XHTML

  1. XHTML Validators
  2. How to Validate Your Code
  3. Trouble-Shooting Invalid XHTML

Quick: What's the first thing that every XHTML document must have?

That's right; every XHTML document must have a DOCTYPE declaration at the very top. This declaration states what language we are working with (in this case XHTML 1.0 Transitional) and where a DTD, or Document Type Definition, for that language might be found. Because the DTD is a set of rules governing how the language can be used, we can take that DTD and check our XHTML against it to make sure we've written it correctly. Thankfully we don't have to check it ourselves; we can use a validator to automatically compare our XHTML to the rules of the DTD and find out if it validates.

XHTML Validators

There are several validators out there that you can use, but the one I recommend is the online validator provided by the World Wide Web Consortium (W3C). Their validator lives at http://validator.w3.org, and you can test your document by uploading it to their server.

How to Validate Your Code

Once you've finished writing your XHTML you'll first want to do a manual check of your document to make sure that all the rules and requirements of XHTML have been followed. This can save you a lot of headache.

To validate your XHTML:

  1. Go to http://validator.w3.org/
  2. Choose Validate by File Upload
  3. Click Browse and find your XHTML file on your local computer's hard drive
  4. Select your XHTML file (remember, it must have the .html extension) and click Open
  5. Click the Check button on the validator

This should result in one of several messages:

  1. This page is Valid XHTML 1.0 Transitional!
  2. This page is tentatively Valid XHTML 1.0 Transitional!
  3. This page is not Valid XHTML 1.0 Transitional!

The first message means you're good to go! The second message means you might be missing something minor, such as a character identifier. The third message means you've broken one or more rules of XHTML, and you need to find and correct those errors. Good thing the validator helps us find those.

Trouble-Shooting Invalid XHTML

If your page is not valid, you should get messages which direct you to the source of the problem. Here's one error I got:

Note that the message tells me which line that the problem is at or near. This can help me find the problem in my XHTML.

The message also tries its best to describe the problem and guess at a cause. Here's my XHTML starting at line 42; based on this validation message can you spot what's wrong?

Did you find a violation of one of our requirements for XHTML? Here's the list of basic XHTML requirements again:

  1. All XHTML documents must have specific elements
  2. All XHTML tag names & attribute names must be in lowercase
  3. All XHTML elements must close
  4. All XHTML elements must be properly nested
  5. All XHTML attribute values must be quoted

All XHTML elements must close. We can assume that the P element shown here doesn't close. If that particular P does close later in the document, we need to move it up above the opening UL, because a P element may not contain a UL element.

I fixed that problem, and ran it through the validator a second time, resulting in:

Even though the validator originally said I had 18 problems, they really all originated with that one un-closed P element! Fixing just that one problem corrected all the other problems in the document.

return to top