Jump to content

Recommended Posts

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 &amp; coktails">
                  </label></td>
                  <td>Martinis &amp; 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.
Link to comment
https://forums.phpfreaks.com/topic/16575-form-checking/
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/16575-form-checking/#findComment-69459
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/16575-form-checking/#findComment-69489
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/16575-form-checking/#findComment-69758
Share on other sites

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...
Link to comment
https://forums.phpfreaks.com/topic/16575-form-checking/#findComment-69881
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/16575-form-checking/#findComment-72362
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.