Tham khảo sách 'javascript bible_ chapter 37', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Data-Entry Validation Give users a field in which to enter data and you can be sure that some users will enter the wrong kind of data. Often the mistake is accidental a slip of the pinkie on the keyboard other times the incorrect entry is made intentionally to test the robustness of your application. Whether you solicit a user s entry for client-side scripting purposes or for input into a server-based CGI or database you should use JavaScript on the client to handle validation of the user s entry. Even for a form connected to a CGI script it s far more efficient from bandwidth server load and execution speed perspectives to let client-side JavaScript get the data straight before your server program deals with it. Real-time versus Batch Validation You have two opportunities to perform data-entry validation in a form as the user enters data into a field and just before the form is submitted. I recommend you do both. Real-time validation triggers The most convenient time to catch an error is immediately after the user has made it. Especially for a long form that requests a wide variety of information you can make the user s experience less frustrating if you catch an entry mistake just after the user has entered the information his or her attention is already focused on the nature of the content or some paper source material may already be in front of the user . It is much easier for the user to address the same information entry right away. A valid question for the page author is how to trigger the real-time validation. Text boxes have two potential event handlers for this purpose onChange and onBlur . I personally avoid onBlur event handlers especially ones that could display an alert dialog box as a data-entry validation is likely to do . Because a good validation routine brings focus to the errant text box you can get some odd behavior with the interaction of the focus method and the In This Chapter Validating data as it is being entered Validating data .