Universal Form Validation - First, the Forms
(Page 2 of 5 )
Let’s have a quick preview of what the code will now be in all forms to ensure the script will work.
<script language="JavaScript"
type="text/javascript"
src="/includes/forms.js">
</script>
<form name="form1" action="/mailer.asp"
method="post" onsubmit="return
checkForm( document.form1 )">
<input type="text" name="name_required" />
<input type="text" name="email_required" />
<input type="text" name="age_required"
onkeypress="return checkInt( event )" />
<input type="text" name="comments" />
<input type="submit" />
</form>
So you’ve cleverly deuced that the JavaScript functions will be contained in one big container file called ‘forms.js’. Whenever we have a form that needs validating, all that need be done is a simple inclusion of this script file, and the attaching of ‘_required’ to the end of any… you guessed it, required fields! Oh, and one more thing.
We need to tell the client browser that when the user is done filling out the form, please verify it before sending to the server. This is accomplished by the onsubmit handler.
But what was that whole mess with the age field? We’ll deal with that a little bit later, for now let’s delve into the workings of the actual checkForm function.
Next: Good Form, Jack! >>
More JavaScript Articles
More By Justin Cook