SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    UK
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checkbox validation help

    I need a bit of help validating a form to make sure that at least one checkbox was selected before the form is submitted. The prpoblem I've got though is that each checkbox has to have a value such as:

    name="checkbox[]" (because it's used to pass values to another PHP page.)

    I''ve come up with the following code so far which works fine if the checkboxes do not have the additional '[]'s after the name... but it doesn't work in this current state:

    PHP
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function valid()
    {
    var i;
    var isValid = false;
    for (i = 0; i < document.the_form.checkbox.length; i++) {
    if(document.the_form.checkbox[i].checked == true) {
    isValid = true;
    }
    }
    if (!isValid) {
    alert("You must select at least one option to continue.");
    return false;
    }
    }
    </script>
    </head>

    <body>
    <form name="the_form" onsubmit="return valid();">
    <table>
    <tr><td><input type="submit" value="Create Record" /></td></tr>
    <tr><td> </td></tr>
    <tr>
    <td>Check box 1</td>
    <td><input type="checkbox" value="1" name="checkbox[]" /></td>
    </tr>
    <tr>
    <td>Check box 2</td>
    <td><input type="checkbox" value="2" name="checkbox[]" /></td>
    </tr>
    <tr>
    <td>Check box 3</td>
    <td><input type="checkbox" value="3" name="checkbox[]" /></td>
    </tr>
    </table>
    </form>
    </body>
    </html>

    [/PHP]

    Any help on getting this working would be really appreciated

    Thanks
    Matt

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2002
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Try this..

    No warranty on this but give it a go..


    for (i = 0; i < document.the_form.elements['checkbox[]'].length; i++)
    {
    if(document.the_form.elements['checkbox[]'][i].checked == true)
    {
    isValid = true;
    }
    }

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    UK
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the input so far...

    Still doesn't work though.. Any more suggestions?

  4. #4
    SitePoint Addict
    Join Date
    Aug 2002
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have exactly the same problem.

  5. #5
    SitePoint Addict
    Join Date
    Aug 2002
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey, i found an answer: http://forums.devshed.com/t93992/s.html

  6. #6
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    UK
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kabatak
    Well found dude.... cheers!


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
  •