Validating your pages (the technical bit)

So hopefully now you’re convinced that validation is A Good Thing, have decided which doctype you’re going to use and have resolved never to use quirks mode again. Check, check, check. Ok people, that means enough of the talking. We are finally get to the technical stuff: how to validate pages!

Validating using the W3C validator

The W3C, home of everything web standards, has validation tools for HTML, CSS and RSS. These are all browser based applications and are pretty simple to use. You can provide it a live URL, a HTML file or directly copy and paste markup into the form:
W3C validator

Then hit the check button for a list of errors:
Validation errors

Then fix errors and repeat until the page passes validation:
Passed validation

Other validation tools

HTML Tidy is an open source command line validator that you can run directly from your machine. There’s a Firefox extension that runs tidy from inside Firefox or you can use it online like the W3C validator. There are also wrappers for it in almost every language, including C#.

The ASP.NET Validation module is an ASP.NET module that you can add to your development site. It automatically validates your pages as you test the site and flags any validation errors that it finds.

ASP.NET validation module

Visual Studio HTML validation is pants

Visual studio also has some built in validation support. Unfortunately it’s not very helpful because it won’t check HTML that’s generated by server controls. It’s also has a bit of a problem with false positives, pages that will be valid when the page is assembled in the browser but aren’t valid in visual studio. VS2005 in particular generates lots of (brain dead) errors that are caused by incomplete HTML that will be generated dynamically at runtime.

This means you can get a million rubbish HTML validation errors that make it hard to find real compilation errors:
VS2005 validation errors

The other problem is that VS2005 doesn’t automatically pick up the type of HTML that you’re using from the doctype of your page, so it might not even be validating your page with the right rules. You have to manually set the schema to validate against:
The dropdown is on the toolbar

If this drives you mad (it drives me mad) it’s possible to completely disable it in the options (tools->options->text editor->HTML->validation)
Uncheck the show errors checkbox

When should you validate?

One last bit of well meaning advice: make validation one of your everyday development tools. Make running your pages through a validator one of the steps of making a major change to the HTML or CSS of your site.

This is particularly important if you’re building a new site. It’s a lot easier to fix simple validation errors as you build the site than it is to fix heaps of complicated ones when you are finished. Think of it in the same way as bug fixing. It is easy to fix a bug in code that you have just written in a block of code that otherwise works. It’s hard to fix bugs in code that you wrote a long time ago and it’s hard to maintain code that contains lots of bugs.

It’s also great for debugging. If you’re having weird browser problems, letting the validator check for syntax mistakes first can help you find problems that have an easy fix (or at least give you good reason to feel aggrieved at browser incompatibilities).

Posted on 13 Jan 08 by Helen Emerson (last updated on 13 Jan 08).
Filed under ASP.NET