Share this Page URL

Chapter 18. More JavaScript Fun > JavaScript and Forms - Pg. 213

More JavaScript Fun 213 What's happening here is that the Validate() function checks the value the user entered into the specified field (Email, in this case). If the value is empty (""), it means the user didn't enter a value. So the script displays an alert (see Figure 18.3), puts the cursor back in the Email field (this is called setting the "focus" in programming parlance), and then returns false, which tells the browser not to submit the form. Figure 18.3. If the user doesn't enter anything in the text box, she sees this message. To use this script on your own form, there are two things you need to adjust: · The if statement checks the value of the form field named Email. When setting this up for your own use, you need to change Email to the name of your field. · The same goes for the frm.Email.focus() statement. That is, you need to change Email to the name of your field. Webmaster Wisdom The user can easily thwart this script by entering a trivial value (such as a single letter) in the text box. My site offers a more sophisticated version of the script that checks to see if the user entered the @ sign (which is part of every e-mail address). See the following page: Confirming Form Data with the User It's important that the data submitted in a form be as accurate as possible. However, lots of web surfers have short attention spans (present company excepted, of course), so they tend to fill in form data haphazardly. To help boost the accuracy of submissions, it's a good idea to display the entered data to the user before submitting it. If everything looks good, the user can submit the data; otherwise, he or she can cancel and make changes. Before getting to the JavaScript, let's set up a sample form that includes the four main form controls: a text box, an option list, radio buttons, and a check box (see Chapter 12). Here it is (see jscheck.htm on the CD in this book): <FORM ACTION="" METHOD="POST" NAME="MyForm"