1150 Part V . Putting JavaScript to Work (My web site)
1150 Part V . Putting JavaScript to Work onBlur event handler. Users who wish to continue past an invalid field are locked in a seemingly endless loop. The problem with using onChangeas the validation trigger is that a user can defeat the validation. A change event occurs only when the text of a field indeed changes when the user tabs or clicks out of the field. If the user is alerted about some bad entry in a field and doesn t fix the error, the change event doesn t fire again. In some respects, this is good because a user may have a legitimate reason for passing by a particular form field initially with the intention of returning to the entry later. Because a user can defeat the onChangeevent handler trigger, I recommend you also perform batch validation prior to submission. In NN4+ and IE4+, you also have the luxury of letting keyboard events trigger validations. This is most helpful when you want to prevent some character(s) from being entered into a field. For example, if a field is supposed to contain only a positive integer value, you can use the onKeyPress event handler of the text box to verify that the character just typed is a number. If the character is not a number, the event is trapped and no character reaches the text box. You should also alert the user in some way about what s going on. Listing 43-1 demonstrates a simplified version of this kind of keyboard trapping, compatible with NN4+ and IE4+ event models. The message to the user is displayed in the statusbar. Displaying the message there has the advantage of being less intrusive than an alert dialog box (and keeps the text insertion cursor in the text box), but it also means that users might not see the message. The onSubmitevent handler in the listing prevents a press of the Enter key in this one-field form from reloading this sample page. Listing 43-1: Allowing Only Numbers into a Text Box
Letting Only Numbers Pass to a Form Field
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.