RE: JavaScript to validate checkboxes

by "Keith Purtell" <kpurtell(at)vantagemed.com>

 Date:  Fri, 25 Oct 2002 11:37:30 -0500
 To:  <thewolves(at)bigfoot.com>
 Cc:  <hwg-languages-digest(at)mail.hwg.org>
 In-Reply-To:  attbi
  todo: View Thread, Original
Well that solved my problem, although I didn't follow your suggestion to the letter. I didn't
understand why I would send the entire form (contains more than a dozen different fields) to
checkffDefine, so I'm just sending the checkboxes through the function you wrote. Thanks so much for
the speedy fix!

Keith Purtell, Web/Network Administrator
VantageMed Operations (Kansas City)
Email:  kpurtell(at)vantagemed.com

CONFIDENTIALITY NOTICE: This email message, including any attachments, is for the sole use of the
intended recipient(s) and may contain confidential and privileged information. Any unauthorized
review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please
contact the sender by reply email and destroy all copies of the original message.


-----Original Message-----
From: Larry Coats [mailto:thewolves(at)attbi.com]
Sent: Friday, October 25, 2002 10:36 AM
To: Keith Purtell
Cc: hwg-languages-digest(at)mail.hwg.org
Subject: Re: JavaScript to validate checkboxes


Keith, if I'm understanding what you're wanting, it appears that you
have a set of at least two checkboxes with the same name, and you want
to require the user to check at least one checkbox in the group. That
is, I assume you don't want to require that ALL of the checkboxes be
checked (doesn't seem to be much point in that). I also assume that you
want to allow more than one to be checked (otherwise it seems like radio
buttons would be more appropriate).

Since you have several checkboxes with the same name, there is an array
of checkbox objects rather than a single checkbox object. Each object in
the array has its own "checked" property. You'll want to verify that one
or more of these objects has the checked property set.

I'd suggest passing the entire form to checkffDefine:

    why += checkffDefine (form);

Then change checkffDefine to something like this:

function checkffDefine (form)
{
   var error = "";
   var count = 0;
   for (var i = 0 ; i < form.ffDefine.length ; ++i) {
     if (form.ffDefine[i].checked) { ++count; }
   }
   if (count == 0) {
     error = "You did not enter an Enhancement Definition.\n";
   }
   return error;
}

Hope that helps!

Larry Coats



Keith Purtell wrote:
> This JavaScript to validate a form works for all input fields except my checkboxes. What the user
> should see when they click the submit button -- assuming they leave form fields blank -- is an
alert
> box with short messages indicating which fields were left blank. Each of those form fields has
it's
> own function and "why +=" line, but I removed most of those for the sake of brevity. Here are
> snippets; I'd like suggestions on how to make this work correctly. TIA.
>
> function checkffDefine (ffDefine) {
> var error = "";
> if (ffDefine == false) {
> 	error = "You did not enter an Enhancement Definition.\n";
> 	}
> return error;
> }
> function checkWholeForm(form) {
>     var why = "";
>     why += checkffDefine(form.ffDefine.checked);
>     if (why != "") {
>        alert(why);
>        return false;
> 	   }
> return true;
> }
>
> FORM NAME="form" ACTION="page2.htm" METHOD="post" onSubmit="return checkWholeForm(this);"
>
> INPUT TYPE="checkbox" NAME="ffDefine" VALUE="ChangeUserInterface"
>  Does the user interface need to change?
> INPUT TYPE="checkbox" NAME="ffDefine" VALUE="CaptureNewData"
>  Is there new data we need to capture?




---
[This E-mail scanned for viruses by Declude Virus]

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA