Best web hosting site - 345Chapter 15Generic HTML Element ObjectsCommon keyboard event tasksWinIE4+
345Chapter 15Generic HTML Element ObjectsCommon keyboard event tasksWinIE4+ enables you to modify the character that a user who is editing a text box enters. Theonkeypressevent handler can modify the event.keyCodeproperty and allow the event tocontinue (in other words, don t evaluate to returnfalseor set the event.returnValueproperty to false). The following IE function (invoked by an onkeypressevent handler) makes sure text entered into a text field is all uppercase, even if you type it as lowercase: function assureUpper() { if (event.keyCode >= 97 && event.keyCode <= 122) { event.keyCode = event.keyCode 32; } } Doing this might confuse (or frustrate) users, so think carefully before implementing such a plan. To prevent a keyboard key press from becoming a typed character in a text field, theonkeypressevent handler prevents the default action of the event. For example, the fol- lowing HTML page shows how to inspect a text field s entry for numbers only:
Whenever a user enters a non-number, the user receives a warning and the character is notappended to the text box s text. Keyboard events also enable you to script the submission of a form when a user presses theEnter (Return on the Mac) key within a text box. The ASCII value of the Enter/Return key is13. Therefore, you can examine each key press in a text box and submit the form whenevervalue 13 arrives, as shown in the following function: function checkForEnter(evt) { evt = (evt) ? evt : event; var charCode = (evt.charCode) ? evt.charCode : (( evt.which) ? evt.which : evt.keyCode); if (charCode == 13) { document.forms[0].submit(); return false; elementObject.onkeydown