Question: I have some information to enter in a text box. The data is structured and only some sequences are valid. For a phone number, for instance, a letter such as `A` is invalid.

So should I intercept keyboard events and prevent typed letters from reaching the text box, or should I draw some red squiggly lines (or another visual cue) to let my user know the letter is not acceptable? Which is a better user experience?

Answer: 
Being on both ends of using a form, here are some guidelines I prefer:

* User inputs should hardly ever be prevented. Meaning if I type the letter 'A' in a phone number field, I would expect to see that 'A'.
* I begin my text field validation on a `onBlur()` event, and no sooner than that. This gives the user a chance to fix that mis-typed 'A' before I show an error message. Allowing the user to correct an error while still in the input field provides one less chance for an obtrusive "hey, you messed up, dude" kind of message.
As with most things..depends on context. I have a more specialized take on the topic. Most of the applications I, or my group, work on are technical in nature and are only used by the reasonably trained. As such we can place more responsibility and freedom in the hands of the users than you could with the general public. Briefly, some of the guidelines we use...

* Disregard any invalid entry....block invalid keystrokes.
* Indicate invalid fields, a state that
should be all but impossible, by
contrast and color\*.
* Focus is always under user control.
Do not trap the user or prevent
a change of focus.
* Never throw a modal warning/error
box. Ever.
* Never ask the user questions. Software is a tool not a person. Yes I am sure.
* The Enter/Return keys always mean:
Accept changes and move on. Do not
change this by using focus on a
cancel/exit button as the default on
Enter.
* The Escape key always means: Discard
and exit.
* Provide auto-completion whenever
possible.
* Provide short-cut entry for numerics
that have a min/max limit. e.g. If a
valid value is between 0.12 and 100.387 then
entries greater that 100.387, like 999, are taken as 100.387, etc.
* Establish very clear black and white
rules on behavior..rules the user can
understand. If this becomes overly complex...re-think it.

Almost all of the examples are user requested and, yes...implementation is sometimes a real bear/nightmare/killer but rarely impossible.

Caveat: *Again, these examples are given in the context of a vertical system with a technical user base....not a web general public form, etc. Your mileage my vary.*

\*Except red. In our business red means: Danger you could be killed...or worse.