SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Free me php klassicd's Avatar
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check checkbox array if one is checked.

    I need to cycle through an array of checkboxs and see if atleast one is checked. Can someone correct my code below?

    PHP Code:
    <script language="JavaScript" type="text/JavaScript">
    function 
    checkForm()
    {
    if (!
    document.test.bt[0].checked && !document.test.bt[1].checked && 
    !
    document.test.bt[2].checked && !document.test.bt[3].checked ) { 
    alert('Business Type is required.');
    return 
    false;
    }
    }
    </script>

    <form action="test.html" method="post" name="test" onSubmit="return checkform()">
    <input name="bt[]" type="checkbox" value="1">
    <input name="bt[]" type="checkbox" value="2">
    <input name="bt[]" type="checkbox" value="3">
    <input name="bt[]" type="checkbox" value="5">
    <input name="Submit" type="submit" value="Submit">
    </form> 

  2. #2
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if ((window.document.test.checkbox1.checked == false) && (window.document.test.checkbox2.checked == false)) && (window.document.test.checkbox3.checked == false) && (window.document.test.checkbox4.checked == false) && (window.document.test.checkbox5.checked == false)
    {
    alert("Business Type is required.");
    return false;

    It's as simple as that.

  3. #3
    Free me php klassicd's Avatar
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <script language="JavaScript" type="text/JavaScript">
    function 
    checkForm()
    {
    if ((
    window.document.test.checkbox1.checked == false) && (window.document.test.checkbox2.checked == false)) && (window.document.test.checkbox3.checked == false) && (window.document.test.checkbox4.checked == false)
    {
    alert('Business Type is required.');
    return 
    false;
    }
    }
    </script>
    <form action="test.html" method="post" name="test" onSubmit="return checkform()">
    <input name="bt" type="checkbox" value="1">
    <input name="bt" type="checkbox" value="2">
    <input name="bt" type="checkbox" value="3">
    <input name="bt" type="checkbox" value="4">
    <input name="Submit" type="submit" value="Submit">
    </form> 
    Still doesnt work. I was under the impression that if i made the name bt[] then it would form an array so I would access it by bt[0] and so on?

  4. #4
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You shouldn't use the same names with checkboxes...only with radio buttons should you use a particular name, like "bt". What you need to do with the checkboxes is name them "checkbox1", "checkbox2", "checkbox3", and "checkbox4". I'm sorry if I didn't make this clearer in my previous post.


Bookmarks

Posting Permissions

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