Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Using JavaScript and forms

Of all the hats JavaScript can wear, its form processing features are among the most sought and used

  • Print
  • Feedback

Page 7 of 7

Most form validation chores revolve around basic data checking: does the user remember to fill in an box? Is it have the right length? Does it contain valid characters? With most forms you can readily answer these questions with a small handful of validation routines.

A typical validation routine is determining if an input box contains only numeric digits, shown below. If the entry contains non-numeric characters, you can ask the user to enter the correct data. A ready-made routine for this is the isNumberString function, which returns the value 1 if the string contains only numbers, and 0 if it contains any non-numeric characters. To use it, provide the data string as the parameter. The value returned by the function tells you if the data is valid.

Listing 9. valid_simple.html

<HTML>
<HEAD>
<TITLE>Test Input Validation</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function testResults (form) {
    TestVar = isNumberString (form.inputbox.value)
    if (TestVar == 1)
        alert ("Congratulations! You entered only numbers");
    else
        alert ("Boo! You entered a string with non-numbers characters");
}

function isNumberString (InString) { if(InString.length==0) return (false); var RefString="1234567890"; for (Count=0; Count < InString.length; Count++) { TempChar= InString.substring (Count, Count+1); if (RefString.indexOf (TempChar, 0)==-1) return (false); } return (true); } </SCRIPT> </HEAD> <BODY> <FORM NAME="myform"> Enter a string with numbers only: <INPUT TYPE="text" NAME="inputbox" VALUE=""> <INPUT TYPE="button" NAME="button" Value="Click" onClick="testResults(this.form)" > </FORM> </BODY> </HTML>

Conclusion

The traditional way to use forms on a Web page is to send all the user's entries to a CGI script or program running on the server. The bulk of most any CGI program is verifying that the user entered valid data. Entry validation is something JavaScript can do, and do very easily. By using JavaScript you can greatly reduce the complexity of writing and implementing CGI programs. And, in many cases JavaScript, can completely supplant CGI programs, taking the burden off the server by relieving it of mundane processing chores.

About the author

Gordon McComb is an author, consultant, and lecturer. He has written 50 books and over a thousand magazine articles during his 20 years as a professional writer. More than a million copies of his books are in print. Gordon also writes a weekly syndicated newspaper column on computers, reaching several million readers worldwide. Gordon's latest book is The JavaScript Sourcebook, forthcoming from Wiley Computer Publishing.

Read more about Tools & Methods in JavaWorld's Tools & Methods section.

  • Print
  • Feedback