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: 
Overriding a user's control of his or her computer will always be an interruption, and often be perceived as an invasion, of that user's workflow. The web site "isn't supposed to" have control of our computers, so exerting such control will be at least a little alarming.

That said, you broach a really important topic, being the importance of letting a user know their entry is invalid before they fill out an entire set of fields and submit them.

The approach I've found to be the least-intrusive but provide the quickest turn-around response is the flow below. 

![alt text](https://i.stack.imgur.com/VxAhU.png)

Essentially, wait for them to fill out their first field, then as they click/tab around a new field (or a field that has been invalidated), check the field that was just completed. If the field that was just completed is valid, print an unobtrusive "good" icon next to it (green, checkmark, happy face, you get the idea). If it fails the validation rules, discretely print a "bad" icon (red, "X", etc.) and a short line about what failed. 

But remember, do not interrupt their workflow: 

* No pop-ups
* no overlays

When they're finished with the field they moved to, they'll either go back to the invalid field or move on and go back later. Either way, they'll see that they have an edit to make but can do it on their own time.

In the end, they'll have a completely valid form before ever submitting, which means 

1. They'll only have to submit once
2. They don't spend time getting mad or frustrated when their submission "fails"
3. They'll like your site more
4. Everyone will have cake
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.