Skip to the Main Content

Note:These pages make extensive use of the latest XHTML and CSS Standards. They ought to look great in any standards-compliant modern browser. Unfortunately, they will probably look horrible in older browsers, like Netscape 4.x and IE 4.x. Moreover, many posts use MathML, which is, currently only supported in Mozilla. My best suggestion (and you will thank me when surfing an ever-increasing number of sites on the web which have been crafted to use the new standards) is to upgrade to the latest version of your browser. If that's not possible, consider moving to the Standards-compliant and open-source Mozilla browser.

May 3, 2003

Validating Comments


The plugin has a new home page. Look there for the most up-to-date installation instructions.

I actually got a couple of emails about my previous post on implementing Comment Validation in a MovableType blog. Evidently, there’s some interest out there. So here is the first in an occasional series of “How-To” articles on MovableType.

Alexei Kosut wrapped the W3C Validator in an MT plugin. I extended his plugin by adding some more MT Template tags, correcting some errors where the Validator was sending invalid output and customized the Validator’s output templates to make them more suitable for inclusion in a weblog comment preview window. More recently (5/26/2004), I updated the plugin to incorporate the newer, more user-friendly error-reporting in the new W3C Validator and added (6/22/2004) some portability fixes. Since Alexei seems to have lost interest, and since my changes are now quite substantial, you can now get my revised plugin with all the changes rolled in.

The plugin has a bunch of prerequisites. You may need to install or update several Perl modules:

  • Config::General (version 2.06 or later)
  • File::Spec (version 0.8 or later)
  • HTML::Entities
  • HTML::Parser (version 3.25 or later)
  • HTML::Template (version 2.6 or later)
  • IO::File
  • IPC::Open3
  • Set::IntSpan
  • Text::Iconv
  • Text::Wrap

If you’re not already using the CPAN module, then perhaps now would be a good time. With CPAN, you can install the whole lot with a single command,

install  HTML::Template Bundle::W3C::Validator

or, if you have most of what you need already, you can install just the missing modules with an appropriate “install ...” command.

“But wait!” I hear you cry, “I don’t have root on my webhost. How can I use CPAN?” Well, the folks who designed it were helpful enough to design it so that you can also install files locally (in your MT/extlib directory):

perl -MCPAN -e shell
o conf makepl_arg "LIB=/your/path/to/MT/extlib \
INSTALLMAN1DIR=/dev/null \
install HTML::Template Bundle::W3C::Validator

CPAN puts the modules through a rigorous set of self-test before installing them. Even if a module passes every single self-test but one, CPAN will err on the side of caution and not install it. For your purposes, this is way more conservative than you need. If a module fails a couple of tests, but passes almost all of the rest, just do a

force install ModuleName

That is pretty sure to be safe.

You may also need a copy of the onsgmls program from the OpenSP Project. It comes pre-installed with most flavours of Linux. For MacOSX, I got mine from Fink (“fink install opensp3”). If worst comes to worst, you can download and compile the OpenSP source. In any case, you may have to edit /plugins/validator/config/validator.conf to point to the location of the onsgmls executable. In my case, it reads

SGML_Parser  = /sw/bin/onsgmls

Now that you have the SGML parser installed, you need a set of DTDs for it to work with. Download and unpack sgml-lib and install it as plugins/validator/sgml-lib .

One more gotcha has to do with whether, in Perl, $0 returns just the filename or the full path on your operating system. If the Validator complains about being unable to find its configuration files, set the path to your plugins directory

PluginPath  /full/path/to/plugins

in mt.cfg and the error will go away.

The output of the Validator is controlled by a bunch of template files in plugins/validator/templates/. The relevant ones are probably results.html (the main template controlling the output), valid.html (included if the page validated) and invalid.html (included if there were errors). You’ll want to tweak them so that the output looks like what you want. And you’re free to use CSS to style them as you wish.

But first, we need to modify our MovableType templates. The key one will be the Comment Preview Template. Let me step you through mine.

<$MTInclude module="DTD"$>
<html xmlns="">
<title><$MTBlogName$>: Comment Preview</title>
<$MTInclude module="HeaderLinks"$>
<$MTInclude module="SmallBanner"$>
<div class="blog">
<h2 class="comments-head">Preview Your Comment</h2>

As you can see, a lot of stuff in my templates is abstracted into modules. I can change the DTD of all the pages of my blog by editing one template module. That module currently reads:

<?xml version='1.0' encoding='iso-8859-1'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "" >

Being able to change something by editing one template module and having the change reflected blog-wide is a very good thing.

<$MTInclude module="DTD"$>
<html xmlns="">
<title>Your Comment</title>
<h3 class="title"><$MTCommentPreviewSubject strip_controlchars="2" remove_html="1"$></h3>
<$MTCommentPreviewBody safe_urls="1" smarty_pants="2"$>
<p class="comments-post">Submitted by <$MTCommentPreviewAuthorLink spam_protect="1"$> at <$MTCommentPreviewDate$></p>
<form method="post" action="<$MTCGIPath$><$MTCommentScript$>">

The contents of the <MTValidate>...</MTValidate> gets passed to the Validator. Note that the Validator expects a fully-formed (X)HTML document, right down to the DTD. Also, I happen to be using the SmartyPants and safe_href plugins.

If the comment had errors, we’ll get a sad message listing the errors. If it was valid XHTML, we’ll get a happy message and then we’ll want to present the user with a preview of his comment and an opportunity to post it. The contents of <MTValidateIfValid>...</MTValidateIfValid> only appear if the Validator found no errors.

<div class="blogbody">
<$MTCommentPreviewBody safe_urls="1" smarty_pants="2"$>
<span class="comments-post">Submitted by
<$MTCommentPreviewAuthorLink spam_protect="1"$> at
<fieldset class="blogbody">
  <input type="submit" name="post" value="&nbsp;POST&nbsp;" />

We only want to display the comment preview if the comment was valid XHTML, and the same goes for the “POST” button.

Next, we present the user with the opportunity to edit the comment and preview it again. Note that there are some additional form fields, because we’re using Alexei’s most excellent MTThreadedComments plugin.

<h2 class="comments-head">Edit Your Comment</h2>
<fieldset class="blogbody">
  <input type="hidden" name="entry_id" value="<$MTEntryID$>" />
  <input type="hidden" name="static" value="<$MTCommentPreviewIsStatic$>" />
  <label for="author">Name:</label><br />
  <input id="author" name="author" value="<$MTCommentPreviewAuthor encode_html="1"$>" /><br />
  <label for="email">Email Address:</label><br />
  <input id="email" name="email"  size="30" value="<$MTCommentPreviewEmail encode_html="1"$>" /><br />
  <label for="url">URL:</label><br />
  <input id="url" name="url" size="50" value="<$MTCommentPreviewURL encode_html="1"$>" /><br />
<input type="hidden" name="parent_id" value="<$MTCommentPreviewParentID$>" />
<label for="subject">Subject: </label><br />
<input id="subject" name="subject" size="30"
       value="<$MTCommentPreviewSubject encode_html="1"$>" /><br />
  <label for="text">Comments:</label><br />
  <textarea id="text" name="text" rows="10" cols="48"><$MTCommentPreviewBody convert_breaks="0" encode_html="1"$> </textarea><br />
  <input type="submit" name="preview" value="&nbsp;PREVIEW&nbsp;" />

Note, again, that there’s only a “PREVIEW” button here. We want the commenter to preview the comment again, if it’s been edited.

Finally, if there are any, we’ll present the user with a listing of previous comments and trackbacks (using the MTSimpleComments plugin) on this post. The same template module used to provide this listing is used to list the comments and trackbacks on the individual archive page. To get the conditional display, we use the Compare plugin.

<MTIfGreater a="[MTSimpleCommentCount]" b="0">
<h2 class="comments-head">Previous Comments &amp; Trackbacks</h2>
<MTInclude module="FollowupList>

Now all we need to do is remove the “POST” button from the Comment Listing Template and the Comment Error Template, so that people need to preview their comment (and, in so doing, run it through the Validator) before they can post it.

Try it out! Type a comment and preview it. Try seeing what happens if you stick in some invalid HTML. Nifty, huh?

Update (11/27/2006): More Checks

Version 0.4 of the plugin incorporates a new configuration option in /plugins/validator/config/validator.conf . The onsgmls parser doesn’t really check XHTML documents for well-formedness. To get an extra layer of protection, you can set

XHTML_Check  = 1

which runs the comment through a real XML parser, ensuring that it is well-formed. To use this option, you’ll need the XML::LibXML Perl Module.

The new version also incorporates yet more user-friendly error messages from version 0.74 of the W3C Validator.

Posted by distler at May 3, 2003 11:31 PM

TrackBack URL for this Entry:

16 Comments & 16 Trackbacks

Re: Validating Comments

Hmmm… Don’t mind me, I find this interesting and I’d like to try it out… so below will be some badly formated HTML…

bad formating with bold and italic…

  1. bad list item 1
  2. bad list item 2

Thanks for the test :)
OH WOW! It works even better than I dreamed… maybe when I have a few hours I’ll implement this :)

Posted by: Jai on May 7, 2003 3:39 PM | Permalink | Reply to this
Read the post In brief: bread machine edition
Weblog: dive into mark
Excerpt: I didn't know yeast ever expired. I thought it was like Twinkies, and fruitcake.
Tracked: May 15, 2003 5:06 PM
Read the post Validate MT Comments
Weblog: ecrosstexas: the texas blog
Excerpt: Jacques Distler, Physics Professor at UT Austin provides the first two parts in a series of "How-to" articles on Validating Comments and Bullet-Proofing II in Moveable Type....
Tracked: May 19, 2003 2:41 PM
Read the post Validating Comments in MT
Weblog: Webspiffy: Elsewhere
Excerpt: An awesome modification I'm going to install when I have some free time.
Tracked: September 4, 2003 10:55 PM

Re: Validating Comments

I hope you still read this comment since this post is several months old. Anyway, I’d really like to install Comment Validation on my weblog, but I can’t seem to get all the Perl modules.

I installed CPAN and tried install Config::General and install Text::Iconv, but the result was “Searching for ‘Module::Name’ returned no results. Try a broader search first”. Is there a particular place where I download these modules?

Without these modules I can’t even try installing the Comment Validation, so I’d appreciate any help you could give me.

Posted by: Bas Hamar de la Brethonière on September 23, 2003 10:02 AM | Permalink | Reply to this

Re: Validating Comments

Never mind, I already found the required modules using the Online CPAN Search. Sorry for the useless post.

Posted by: Bas Hamar de la Brethonière on September 23, 2003 10:12 AM | Permalink | Reply to this

Re: Validating Comments

This is not so much a response to this post about validating comments (which by the way is clear cut and concise), but a plea for a similar guided tour through the MTThreadedComments that you mention. I really want that plugin, but somehow it doesn’t work for me…
I’ve cast lines out in the MT Support forum, but no-one has bitten yet.

Posted by: Paul on December 1, 2003 3:12 PM | Permalink | Reply to this
Read the post Time to convert to XHTML 1.1
Weblog: Eclectic Echoes
Excerpt: After the New York trip, I think it will be time to change the site over from XHTML(eXtensible Hypertext Markup Language) 1.0 Transitional to XHTML 1.1. The changes should be limited, mostly changing the javascript and form areas. The main area's right...
Tracked: January 13, 2004 3:46 PM

Re: Validating Comments

I’m going to try one of my favorite messups here.
my link without an ending quote in the ‘a href’ part.

Fascinating. It told me it was valid, but changed my link to <a>my link</a>, leaving out the ‘href’ and actual link.

Posted by: Kathy K on February 7, 2004 6:18 PM | Permalink | Reply to this

Re: Validating Comments

This is just a test

Posted by: jt on February 9, 2004 10:30 PM | Permalink | Reply to this
Read the post Sustaining Validation
Weblog: asterisk*
Excerpt: I don't want to open a can of worms here, but I thought it might be helpful to share my experience over the last week trying to put in place a better system to keep Asterisk valid. Don't worry I wont bore you with all the details. As you may...
Tracked: June 19, 2004 9:49 AM

Re: Validating Comments

Nice updates!
now i think plugin installed

Posted by: Dan on June 22, 2004 5:38 PM | Permalink | Reply to this

Re: Validating Comments

How do you do it with WordPress? are there any such plugins available to validate comments for WordPress?

Posted by: Jax on July 29, 2004 1:26 AM | Permalink | Reply to this

WordPress plugin

To my knowledge, there is currently no WordPress plugin, with comparable functionality, available.

Posted by: Jacques Distler on July 29, 2004 8:51 AM | Permalink | PGP Sig | Reply to this

Re: Validating Comments

What if my name contains an ampersand? Is there anything that we can do to protect our comments from ampersands?

I’m asking because it happened for the first time to me the other day. And once is enough for me to be concerned.

Posted by: Me, Myself & I (Joshua Kaufman) on August 1, 2004 1:41 PM | Permalink | Reply to this

Re: Validating Comments

In short, we don’t f*#$ around checking only some of the form fields; we check all of them.

See my (revised) post on the subject.

Posted by: Jacques Distler on August 2, 2004 12:11 AM | Permalink | Reply to this
Read the post validating MT
Weblog: monologue
Excerpt: W3C Validator plugin for MovableType. One of the more useful plugins in my honest opinion....
Tracked: August 28, 2004 9:51 AM
Read the post Why 'More Than Valid'?
Weblog: More Than Valid
Excerpt: Small Thanks Thank you to Eric Meyer for linking me in his Distractions list, and to those of you who found this article via that link. The site is not live yet, and this is just a sample article so...
Tracked: December 4, 2004 8:55 PM
Read the post Validation!
Weblog: Upon Reflection
Excerpt: This weblog is now Valid XHTML 1.1 plus MathML 2.0! It turned out to go more smoothly than I thought. There were only a few troublesome entries that I really had to struggle with. Most of my entries validated once...
Tracked: December 22, 2004 1:33 AM
Read the post Fear Me, for I Am Root
Weblog: Upon Reflection
Excerpt: Setting up a webserver, posting course material online, and creating a weblog requires a deep exploration of computer geekness. The downside is I am a very reluctant geek. I don't experience a sudden thrill while validating HTML code. I don't...
Tracked: January 7, 2005 11:31 AM
Read the post New Look, XHTML 1.1 and MathML
Weblog: Procrastination
Excerpt: You might have noticed the new look of this weblog. It all started due to Asif and Jacques Distler. Asif just started using LaTeX formulae on his weblog and Jacques has been doing so for quite a while. Asif is...
Tracked: January 9, 2005 11:03 PM

Re: Validating Comments

If worst comes to worst, you can download and compile the OpenSP source. In any case, you may have to edit /plugins/validator/config/validator.conf to point to the location of the onsgmls executable.

One thing you missed there is that the SGML Library Path also needs to be set to the correct location. In fact, that needs to be done whether OpenSP is pre-installed or compiled by you since the default path “validator/sgml-lib” doesn’t exist.

Posted by: Zack on January 19, 2005 2:16 AM | Permalink | PGP Sig | Reply to this



What’s missing is the instruction to install a copy of sgml-lib
in the plugins/validator directory.

Alexei had that instruction in his writeup. I neglected to include it in mime.


Posted by: Jacques Distler on January 19, 2005 9:40 AM | Permalink | PGP Sig | Reply to this

Re: sgml-lib

Thanks. Somehow I missed it in Alexei’s instructions, probably because I was more focussed on yours.

Also, I had to put in the absolute path for sgml-lib in validator.conf instead of a relative path.

Posted by: Zack on January 19, 2005 1:04 PM | Permalink | PGP Sig | Reply to this
Read the post Front-end and Back-end Changes
Weblog: Procrastination
Excerpt: There have been a lot of changes here recently, most of them on the back-end. Most of this work was related to having a bilingual (English and Urdu) blog along with MathML equations. This required valid XHTML 1.1 and serving...
Tracked: February 11, 2005 8:25 AM
Read the post Drupal and XML
Excerpt: Everyone has heard of the latest standard, XHTML. Pretty much every blogger proudly displays his or her tag of validation, maybe even a valid CSS tag as well. That's good and fine, but are you serving the document as an XML document or just as tag soup?
Tracked: July 2, 2005 9:57 AM
Read the post Are You Being Served? -- Part II
Weblog: Upon Reflection
Excerpt: Step 3: MathML At this point you should have valid XHTML 1.1 pages served with the correct MIME type to all the good little browsers.... It is much easier to use some kind of shorthand to enter equations, and let Movable Type convert it to MathML when...
Tracked: September 30, 2005 1:01 PM

Re: Validating Comments

What is a validator ?

Posted by: Bob on February 28, 2006 8:47 AM | Permalink | Reply to this
Read the post Bulletholes
Weblog: Musings
Excerpt: Henri Sivonen bursts my bubble.
Tracked: November 25, 2006 2:38 AM
Read the post New Look, XHTML 1.1 and MathML
Weblog: Procrastination
Excerpt: You might have noticed the new look of this weblog. It all started due to Asif and Jacques Distler. Asif just started using LaTeX formulae on his weblog and Jacques has been doing so for quite a while. Asif is...
Tracked: March 14, 2007 7:39 AM

Re: Validating Comments

I can neither confirm or deny the sexuality of my cat.

Posted by: martha on November 27, 2007 8:35 PM | Permalink | Reply to this
Read the post Are You Being Served? -- Part II
Weblog: Upon Reflection
Excerpt: In Part I, we discussed how to validate your web site and configure your server to send math-able web pages. At this point you should have valid XHTML 1.1 pages served with the correct MIME type to all the good...
Tracked: August 29, 2008 1:48 AM

Post a New Comment