109Chapter 9Forms and Form ElementsListing 9-6 shows a (Professional web hosting)
109Chapter 9Forms and Form ElementsListing 9-6 shows a page with a simple validation routine that ensures all fields have some- thing in them before allowing submission to continue. (The sample form has no actionattribute, so this sample form doesn t get sent to the server.) Notice how the onsubmiteventhandler (which passes a reference to the formobject as a parameter in this case the thiskeyword points to the formobject because its tag holds the event handler) includes thereturnkeyword before the function name. When the function returns its trueor falsevalue, the event handler evaluates to the requisite returntrueor returnfalse. Listing 9-6:Last-Minute Checking Before Form Submission
One quirky bit of behavior involving the submit()method and onsubmitevent handlerneeds explanation. While you might think (and logically so, in my opinion) that the submit() method would be the exact scripted equivalent of a click of a real Submit button, it s not. Thesubmit()method does not cause the form s onsubmitevent handler to fire at all. If you wantto perform validation on a form submitted via the submit()method, invoke the validation inthe script function that ultimately calls the submit()method. So much for the basics of forms and form controls. In the next chapter, you step away fromHTML for a moment to look at more advanced JavaScript core language items: strings, math, and dates.