digitalgod Posted August 4, 2006 Share Posted August 4, 2006 hey guys,was just wondering how can I use javascript to check if a checkbox has been checked.. I have a few checkboxes and I need at least one of them to be checked before submitting the form. Since the name of the checboxes are an array I'm not sure what to do.here's a part of the form[code]<td>*Alcohol:</td> <td><table border="0"> <tr> <td><label> <input name="alcohol[]" type="checkbox" id="alcohol[]" value="beer"> </label></td> <td>Beer</td> </tr> <tr> <td><label> <input name="alcohol[]" type="checkbox" id="alcohol[]" value="wine"> </label></td> <td>Wine</td> </tr> <tr> <td><label> <input name="alcohol[]" type="checkbox" id="alcohol[]" value="mixed drinks"> </label></td> <td>Mixed Drinks </td> </tr> <tr> <td><label> <input name="alcohol[]" type="checkbox" id="alcohol[]" value="martinis & coktails"> </label></td> <td>Martinis & Coktails </td> </tr> </table></td>[/code]and here's what I have so far for the javascript[code]function checkAlcohol(){ var frm = document.forms["form"]; if (frm.alcohol[].checked == false ) { alert('You must select at least one type of alcohol'); return false; } else { return true; }}[/code]that function is called when a user presses submit but it's giving me errors when the pages load. Quote Link to comment https://forums.phpfreaks.com/topic/16575-form-checking/ Share on other sites More sharing options...
dwees Posted August 4, 2006 Share Posted August 4, 2006 Well first you aren't supposed to have multiple elements on the same page with the same ID, use Class for that. Have you looked around carefully for any Validate form scripts? I seem to remember seeing stuff you need all over the place.Dave Quote Link to comment https://forums.phpfreaks.com/topic/16575-form-checking/#findComment-69424 Share on other sites More sharing options...
digitalgod Posted August 4, 2006 Author Share Posted August 4, 2006 I know how to validate a form and the id is an array (actually it becomes an array in my php file when it gets processed), when the form gets processed whatever has been checked is stored in the array. I'm just looking for a way to make sure that at least one checkbox has been checked.I'll google some more I guess Quote Link to comment https://forums.phpfreaks.com/topic/16575-form-checking/#findComment-69459 Share on other sites More sharing options...
digitalgod Posted August 4, 2006 Author Share Posted August 4, 2006 ok tried doing this but it doesn't work either.... any ideas?[code]function checkAlcohol(){ var elements = document.forms["form"].getElementsByName("alcohol[]"); for(var i=0; i < elements.length; i++){ if(elements[i].checked) { checked = true; } } if (!checked) { alert('You have to select at least one type of alcohol.'); } return checked;} [/code] Quote Link to comment https://forums.phpfreaks.com/topic/16575-form-checking/#findComment-69489 Share on other sites More sharing options...
dwees Posted August 5, 2006 Share Posted August 5, 2006 Do you mean the logic of the form checker?I would use something like:[code]function check_form(inputFromForm) { var i=0; var formArray = new Array(); var formFilled = false; switch(formVariable) { case firstValue: formArray[i] = firstValue; i++; firstValue = null; formFilled = true; check_form(inputFromForm); break; case secondValue: formArray[i] = secondValue; i++; firstValue = null; formFilled = true; check_form(inputFromForm); break; case thirdValue: formArray[i] = thirdValue; i++; fthirdValue = null; formFilled = true; check_form(inputFromForm); break; default: formNotFill(formFilled); }}[/code]This calls itself rescursively and checks each input to see if it is filled, and creates an array of the values.It probably needs some polishing.Dave Quote Link to comment https://forums.phpfreaks.com/topic/16575-form-checking/#findComment-69758 Share on other sites More sharing options...
digitalgod Posted August 5, 2006 Author Share Posted August 5, 2006 what I mean is that I have a couple of checkbox "groups" meaning a user could select one or more options for a given category but it has to be at least 1.so let's say in my form I have :Select favorite type of music : and I give the user a choice between 5 types, they all have the same id (mtype[]) then I ask the user Select Alcohol type : I give the user a few choice, they all have the same id (alcohol[]) Now when he/she clicks on submit I want to be able to check whether there's at least 1 option that has been checked for mtype[], and also check if there's at least 1 option that has been checked for alcohol[].but I can't seem to get it to work... Quote Link to comment https://forums.phpfreaks.com/topic/16575-form-checking/#findComment-69881 Share on other sites More sharing options...
digitalgod Posted August 9, 2006 Author Share Posted August 9, 2006 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/16575-form-checking/#findComment-72118 Share on other sites More sharing options...
radalin Posted August 10, 2006 Share Posted August 10, 2006 did you tried something like:[code]var i ;for (i = 0 ; i < mytpe.length ; i++){if (mytype[i].value > 0 )break;}if ( i == mytype.length)alrt("Select at least one music.");[/code]Well I think your checkboxes has values and if they are checked they return that value like "mytype1=1". so if the value is higher then or whatever value you have chosen, then this means that user has selected something.I wrote such code because you said all was added into an array. But I do not clearly get why you are giving same ids to different elements. It will cause confusion for javascript what to look out. Anyway I hope this can help even a bit. Quote Link to comment https://forums.phpfreaks.com/topic/16575-form-checking/#findComment-72362 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.