Re: JavaScript Validation

by Christopher Higgs <chiggs(at)unimelb.edu.au>

 Date:  Wed, 26 Jun 2002 16:15:47 +1000
 To:  thewolves(at)bigfoot.com,
Matthew Ohlman <matthew(at)ohlman.com>
 Cc:  hwg-basics(at)hwg.org
 References:  ohlman
  todo: View Thread, Original

>Matthew Ohlman wrote:
>>Hi list. I have another problem. I have written a JavaScript validator, 
>>but I can't seem to get it to work. I'm pretty sure it is just something 
>>simple, so if somebody could look it over and tell me if they see 
>>anything I would really appreciate it!! Here is the code:

At 10:58 PM 25/06/2002 -0700, Larry Coats wrote:
>When you give your submit button a name of "submit", that actually 
>replaces the form's submit method with an object for the input statement 
>-- at least in Netscape browsers (not sure about IE). I fought this 
>problem for hours a couple of months back before I figured it out, so I'm 
>not likely to forget it! (I was using Netscape 6). There's probably no 
>reason to even bothering to name your submit and reset buttons, unless you 
>have multiple submit buttons and need to be able to detect which one was used.
>
>3rd problem:
>
>I don't think that document.form.submit() is the proper statement to call 
>the form's submit method. The easiest way to do the submit is to replace 
>this statement with a "return true;", and to change the onclick to 
>onclick="return validate()".

Larry is correct that giving the submission button a value of "submit" - 
means it automatically submits regardless of the validation checks.

I was under the impression that replacing this with a button with an 
"onClick" event handler that calls a javascript function was OK, but it 
wouldn't actually submit the form unless the submit method was 
called.  This means those without JavaScript capability (or with the 
functionality disabled for security reasons) cannot submit your form.

The solution was to use a "submit" button, use a validation function that 
gives "return true" and "return false", AND call the validation function ** 
onSubmit ** as an attribute of the FORM tag:
<.FORM name="contact" METHOD="post"
ENCTYPE="TEXT/PLAIN" onSubmit="return validate();">

That way if javascript is disabled, the form is transmitted.  If JavaScript 
is enabled, the form is only submited if validate() returns true.


Chris Higgs
Manager, New Learning Technology
ILFR, University of Melbourne
http://www.landfood.unimelb.edu.au/
Phone: +61 3 8344 9749  Fax: + 61 3 9348 2156

HTML: hwg-basics mailing list archives, maintained by Webmasters @ IWA