| Home | Pauline's Pages | Howto Articles | Uniquely NZ | Small Firms | Search |
| Validation of Form Input Fields |
The following code snippets have been put together to check an email address has been given so one can reply and to trap submissions before the sender has filled in a manditory field or has put just spaces to fool you. It works by calling a validation routine when the form is submited. The onSubmit event handler checks the various fields and a return value of true allows the submit to continue or false leaves one on the page. This version generates an alert in the checking routine if there is a problem. A further problem is not found until the next submission. The JavaScript routines are loosely based on work by McFedries.
JavaScript Snippets to cut and paste for checking a form has a valid email field and that one or more fields have visible characters present<script language="JavaScript" type="text/javascript"><!-- // Simple check for valid Email address in string
function valid_email(email_address) { //Check for empty or only non visible characters in string
function its_not_visible(string_value) {
 for (var counter = 0; counter < string_value.length; counter++) //Submit handler can be extended to many more fields
function submit_handler() {
if( valid_email(document.forms[0].email.value) ) {
The code above should be in the head and the submit_handler() function is called as below. Note the return is essential in the onSubmit="return submit_handler().
<FORM METHOD=POST ACTION="http://www.yoursite.com/scripts/form_response.ihtml" onSubmit="return submit_handler()"> The code can be copied and pasted If you copy by hand beware as it is impossible to tell 2x single quotes '' from a double quotes " in many character sets.
|
The following is a "do nothing" test so it is safe to try it with the submit button.
|
Copyright © Peter and Pauline Curtis Content revised: 15th October, 2003 |
|