Share this Page URL

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

More JavaScript Fun onSubmit="return CheckData(MyForm)"> Please enter your name:<BR> <INPUT TYPE="TEXT" NAME="UserName"> <P> Who is your favorite Beatle?<BR> <SELECT NAME="Beatle"> <OPTION VALUE="George" SELECTED>George <OPTION VALUE="John">John <OPTION VALUE="Paul">Paul <OPTION VALUE="Ringo">Ringo <OPTION VALUE="Pete Best">Pete Best <OPTION VALUE="Hunh?">Who the heck are the Beatles? </SELECT> <P> Have you ever gotten jiggy with it?<BR> <INPUT TYPE="RADIO" NAME="Jiggy" VALUE="Yes" CHECKED>Yes <INPUT TYPE="RADIO" NAME="Jiggy" VALUE="No">No <INPUT TYPE="RADIO" NAME="Jiggy" VALUE="Shhh">Not Telling <P> <INPUT TYPE="CHECKBOX" NAME="Spam">Send tons of spam? <P> <INPUT TYPE="SUBMIT" VALUE="Fire!"> </FORM> 214 Note, in particular, that I added the JavaScript onSubmit attribute to the <FORM> tag: onSubmit="return CheckData(MyForm)" This tells the browser that when the user submits the form, it must run the CheckData() JavaScript function. The return part means that the browser should examine the value returned by Check- Data() to see whether the form submission should proceed or be cancelled. Here's the JavaScript: