Example 4—Simple Form Validation

If you have a form of any kind on your Web site, you will probably want to reduce the amount of junk or inaccurate information you receive. One of the quickest ways to do this is by having a script that validates the information sent to you.

Here is a simple example that validates one input box that is expecting to receive some text. If it is empty, it refuses to submit the form and tells the user to enter text (see Figure 15.5):

 <html> <head> <title>A Simple Page</title> <script language="JavaScript"> <!-- Cloaking device on! function sendMe() { return confirm("Do you want to continue the submission?"); } function chkForm() { if (form1.inp1.value == "") { alert("Please fill in the text box"); form1.inp1.focus(); ...

Get JavaScript™ 1.5 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.