Check box validation problem

Results 1 to 4 of 4

Thread: Check box validation problem

  1. #1
    Kate Guest

    Default Check box validation problem

    Hi,<BR>Im having a strange problem with a page. Basically I output a varying amount of records (0-20) with check boxes against each record. Everything is working fine except when I have only ONE record to display. What happens is when I check the box it displays an error message saying I havent actually checked anything which is not true as I have...Can anyone help...If you need further info please mail me...Heres my code that checks to ensure a box is checked...<BR><BR><BR>function imgAssign_onclick() <BR>{<BR>var checkSelected = false;<BR>alert(frmTeamQueue.Task.length);<BR> for (i = 0; i &#060; frmTeamQueue.Task.length; i++)<BR> {<BR> if (frmTeamQueue.Task[i].checked == true)<BR> checkSelected = true;<BR> }<BR> if (checkSelected == false)<BR> {<BR> alert("You have not selected a task to assign !");<BR> }<BR> if (checkSelected == true)<BR> { <BR> var Assign = false<BR> document.frmTeamQueue.submit(); <BR> }<BR>}<BR>

  2. #2
    Join Date
    Dec 1969

    Default Array vs. non-array...

    When you have more than one checkbox of the same name, then a reference to document.formname.checkboxname indeed gives you an array with that name.<BR><BR>But when you only have one checkbox with a given name, then document.formname.checkboxname actually gives you the checkbox itself! *NOT* an array!<BR><BR>About the best thing you can do is check the type of the object. If it&#039;s an array, go one route. If it&#039;s a single checkbox, go a different route.<BR><BR>And, incidentally, if you don&#039;t use document. in front of your form name your code won&#039;t work on NS.<BR><BR>So:<BR><BR>function imgAssign_onclick() <BR>{<BR>&nbsp; &nbsp; var checkSelected = false;<BR>&nbsp; &nbsp; var cbs = document.frmTeamQueue.Task;<BR>&nbsp; &nbsp; if ( cbs.type == &#039;checkbox&#039; )<BR>&nbsp; &nbsp; {<BR>&nbsp; &nbsp; &nbsp; &nbsp; // handle a single checkbox object <BR>&nbsp; &nbsp; &nbsp; &nbsp; checkSelected = cbs.checked;<BR>&nbsp; &nbsp; } else {<BR>&nbsp; &nbsp; &nbsp; &nbsp; // handle this as an array <BR>&nbsp; &nbsp; &nbsp; &nbsp; for (i = 0; i &#060; cbs.length; i++)<BR>&nbsp; &nbsp; &nbsp; &nbsp; {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( cbs[i].checked ) checkSelected = true;<BR>&nbsp; &nbsp; &nbsp; &nbsp; }<BR>&nbsp; &nbsp; }<BR>&nbsp; &nbsp; ...<BR><BR>

  3. #3
    Joel N Guest

    Default FOILED AGAIN!

    Well, for what it&#039;s worth this was my sample code.<BR><BR>As usual, Bill is correct. One checkbox means NO array of checkboxes.<BR><BR><BR>&#060;html&#062;<BR>&#060;s cript&#062;<BR>function imgAssign_onclick() <BR>{<BR>var checkSelected = false;<BR>if (frmTeamQueue.Task.length)<BR>{<BR> for(i = 0; i &#060; frmTeamQueue.Task.length; i++)<BR> {<BR> if(frmTeamQueue.Task[i].checked == true)<BR> checkSelected = true;<BR> }<BR>}<BR>else<BR>{<BR> if (frmTeamQueue.Task.checked == true)<BR> checkSelected = true;<BR><BR>}<BR><BR>if (checkSelected == false)<BR>{<BR>alert("You have not selected a task to assign !");<BR>}<BR><BR>if (checkSelected == true)<BR>{ <BR>var Assign = false<BR>document.frmTeamQueue.submit(); <BR>}<BR><BR>return checkSelected<BR>}<BR>&#060;/script&#062;<BR>&#060;body&#062;<BR>&#060;form name="frmTeamQueue" onsubmit="return imgAssign_onclick();"&#062;<BR>&#060;input type="checkbox" name="Task" value="Something"&#062;Something<BR><BR>&#060;inpu t type="checkbox" name="Task" value="Something Else"&#062;Something Else<BR><BR>&#060;input type="submit" name="submit it"&#062;<BR>&#060;/form&#062;<BR>&#060;/body&#062;<BR>&#060;/html&#062;

  4. #4
    Join Date
    Dec 1969

    Default Just a minor point...

    Joel copied the form of the original code and did:<BR>&nbsp; &nbsp; if(frmTeamQueue.Task[i].checked == true) <BR><BR>You know, you *really* don&#039;t have to test for == true. Really. <BR><BR>Just doing<BR>&nbsp; &nbsp; if(frmTeamQueue.Task[i].checked) <BR>does exactly the same thing. After all, the .checked property *has* to be either true or false. And so you are, essentially, then doing<BR>&nbsp; &nbsp; if(true)<BR>or<BR>&nbsp; &nbsp; if(false)<BR>which is what you end up doing if you compare via == true, as well.<BR><BR>Save a bit of coding. (And a tiny bit of performance. In true compiled languages you wouldn&#039;t save performance, but with JS you do, as it really does have to interpret the two extra tokens in the byte code.)<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts