Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Flushing the keyboard buffer?

Status
Not open for further replies.

RonHerman

Programmer
Jun 4, 2001
11
US
I am writing a telephone formatting event handler. Currently my function works on the "onKeyUp" event. It checks the string, strips out unwanted characters and rewrites the value of the element. It does this after each "KeyUp", but this creates problems (see last paragraph if you want details)

I'd rather use the "onKeyPress" event and check the value of the key pressed. I tried this, but I was not able to prevent the value of the key from being outputted when it didn't pass the formatting. The javascript flush() function is used for outputting HTML, not flushing the keyboard buffer. So that didn't work. Any ideas here???

For example: Let's say the user wants to change the 3rd and 4th characters (out of 10 characters). After the first change, the function is called and it will rewrite the phone number, placing the cursor at the end of the text box. Then the user has to use his arrow keys or mouse to move the cursor to the next number he wishes to change. This is a bit tedious for the user.

 
Unfortunately, JavaScript does not have the power of defining where a form element is focused. You could try using multiple text boxes or having the script change the telephone number only right at the end:

User types
Code:
555
and the script adds "-" right at the end.

Then, the user can type out the rest of the number. I really don't recommend it, though, as it would do weird formatting right while the user is, say, correcting the number. I think the best idea would be to format the number only right after it has been completely typed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top