Newbie: form validation problem

R

Rob

Hello all, I have a form with a bunch of checkboxes.
When the form is submitted, I need to have at *least* one
checkbox checked. I wrote script to do just that and it works
fine except for one part.
When my form consists of only 1 checkbox, the form validation
script does not work correctly.


function check(){

var counter = 0;
var i;

for(i=0;i<contactsListing.checkbox.length;i++){
if(contactsListing.checkbox.checked){
counter++;
}
}

if(counter == 0){
alert("You must select at least 1 entry.");
return (false);
}
else{
return (true);
}
}

My form looks like this:

<form name="contactsListing" onSubmit="return check()"
method="post" action="http://www.myserver.abc/stuffToDo">
<table width=300>
<tr><td>
<input type="checkbox" name=checkbox value="001"> 001
</td></tr>
</table>
<input type="submit" name="submit" value="Delete">
</form>

Checking the only available box and clicking the 'Delete' button
gives me a "You must select at least 1 entry" alert.

What am I doing wrong?

Thanks for any pointers,

-Rob
 
L

Lasse Reichstein Nielsen

Rob said:
Hello all, I have a form with a bunch of checkboxes.
When the form is submitted, I need to have at *least* one
checkbox checked. I wrote script to do just that and it works
fine except for one part.
When my form consists of only 1 checkbox, the form validation
script does not work correctly.

When you have more than one form element with the same name,
accessing them by name returns an array. Your script depends
on this (that is where the "length" property comes from).

When only one element has a name, there is no array, and no
property called "length". You need to make a special case
for this case.

/l
 
R

Rob

When you have more than one form element with the same name,
accessing them by name returns an array. Your script depends
on this (that is where the "length" property comes from).

When only one element has a name, there is no array, and no
property called "length". You need to make a special case
for this case.

/l

ok, so how does one go about discovering whether there is an
array. is there some sort of <element>.isArray() ? method I can
use to see if msg_id is an array or a single element?

-Rob
 
R

Rob

ok, so how does one go about discovering whether there is an
array. is there some sort of <element>.isArray() ? method I can
use to see if msg_id is an array or a single element?

Ok, I think I found an answer. I need to check the element for
the length property. if it returns undefined, it is only a
single element. such as this piece of code I found on the 'net:

if (typeof(someElement.length) == "undefined"){
//single element
}
else{
//array of elements
}

-Rob
 
R

Richard Hockey

Rob said:
Hello all, I have a form with a bunch of checkboxes.
When the form is submitted, I need to have at *least* one
checkbox checked. I wrote script to do just that and it works
fine except for one part.
When my form consists of only 1 checkbox, the form validation
script does not work correctly.

As mentioned in one of the other replies, if you have multiple checkboxes
with the same name an array is generated, but if you have only one checkbox
the array is not created.
My form looks like this:

<form name="contactsListing" onSubmit="return check()"
method="post" action="http://www.myserver.abc/stuffToDo">
<table width=300>
<tr><td>
<input type="checkbox" name=checkbox value="001"> 001

Personally, I wouldn't use 'checkbox' as a form object name/id attribute,
perphaps something referring to the purpose of the checkbox might be more
appropriate, in the event that you have to come back to this page in nine
months to update it.
</td></tr>
</table>
<input type="submit" name="submit" value="Delete">
</form>

Try something like:

function check()
{
var count=0;
var FormOBject=document.forms['contactsListing'];
var CBObject=FormOBject.elements['checkbox'];

var cblength=CBObject.length;

//check to see if an array has been defined
if(typeof cblength=="undefined")
{
// no array exists, examine 'checked' property of checkbox directly
if(CBObject.checked==true) count++;
}
else
{
// an array has been defined, step through each array element
for(i=0;i<cblength;i++)
{
if(CBObject.checked) count++;
}
}

// check to see if any checkboxes have been selected
if(!count)
{
alert('You have not selected any messages to delete');
return false;
}
return true;
}
 
G

Grant Wagner

Rob said:
Ok, I think I found an answer. I need to check the element for
the length property. if it returns undefined, it is only a
single element. such as this piece of code I found on the 'net:

if (typeof(someElement.length) == "undefined"){
//single element
}
else{
//array of elements
}

-Rob

From your posts I'm assuming you want to be able to test a group (or
possibly one) of same-named input elements on a form. Your code above
demonstrates how to determine the difference, but it also requires the
coding of two separate and distinct handling routines. Here is code that
normalizes any multiple instance input to a collection/array so you just
need one bit of code to handle it:

if (typeof someElement.length == "undefined") {
someElement = [ someElement ];
}

for (var i = 0; i < someElement.length; i++) {
// handle multiple instances
}

Now if there is only one instance of an input with a particular name, it
gets normalized to a collection and you can process it as if there were
1 instance of that named input.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top