[thelist] JavaScript and Form Validation Trickery Not Working

Ben Dyer ben_dyer at imaginuity.com
Fri Jun 7 14:53:01 CDT 2002


Let me preface this by saying my expertise in JavaScript is practically nil.

I've been experimenting with the way I do form validation by changing the
background color of fields that don't validate, like so:

   function verify_form() {

     error_color = '#FFD8D8';

     if (!document.forms[0].txtUsername.value) {
       alert("Please enter a username.");
       document.forms[0].txtUsername.focus();
       document.forms[0].txtUsername.style.backgroundColor = error_color;
       return false;
     }

   }

And then on the form field:

<input type="text" name="txtUsername" id="txtUsername" size="15"
maxlength="15" value="" onchange="this.style.backgroundColor='white'" />

Now then, this correctly turns the field a shade of light red in IE5/6,
Netscape 6 but not in Netscape 4.  I expected it not to work in NN4, that's
not the problem.  The problem is that Netscape 4, does the error message,
focuses on the field, doesn't color it red but skips the return false; line
and submits the form without any further validation.

Is there any way to fix this for NN4 without removing the color change?

--Ben

Ben Dyer, Senior Internet Developer, Imaginuity Interactive
http://www.imaginuity.com/

   Whatever it is, I'm sure that I was just about to get to it.
-----------------------------------------------------------------
   http://members.evolt.org/OKolzig37/     http://www.evolt.org/




More information about the thelist mailing list