#1
  1. No Profile Picture
    Citytech Softwares
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    India
    Posts
    90
    Rep Power
    12

    Javascript validation for dynamic checkboxes


    Hello There,

    Below is an excerpt of the code which I tried to use for my form where checkboxes are being generated dynamically from the database, I want the user to select at least one of the checkbox. I got the below code from one of the threads of Devshed whose url is http://forums.devshed.com/t37766/s.html . Thisworks fine in IE but in Netscape it gives an error in the line where the while statement is written. Please help. Thanks in advance.

    <script type="text/javascript" language="JavaScript">

    var msg = new Object();
    msg['Courses[]'] = 'Please select at least one of the courses from the list.';
    msg['Employee[]'] = 'Please select one or more of the employees listed.';

    function checkform(f) {
    var currEl, currGrp, e = 0;
    while (currEl = f.elements[e++]) {
    if (currEl.type == 'checkbox' && currEl.name) {
    currGrp = f.elements[currEl.name];
    for (var i=0; i<currGrp.length; ++i) if (currGrp[i].checked) break;
    if (i == currGrp.length) {
    alert(msg[currEl.name]);
    currGrp[0].focus();
    return false;
    }
    e += currGrp.length - 1;
    }
    }
    return true;
    }

    </script>
    while statement
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    May 2003
    Posts
    1,014
    Rep Power
    15
    Are you talking about NS4 or a Mozilla based version?

    I tried your code with this sample HTML, and it worked fine in both Mozilla and Netscape 4:
    Code:
    <form onsubmit="return checkform(this)">
    <input type="checkbox" name="Courses[]" value="chk0">
    <input type="checkbox" name="Courses[]" value="chk1">
    <input type="checkbox" name="Courses[]" value="chk2">
    <input type="submit" value="Send now!">
    Please post some of the HTML (or attach it to a post), or a URL with the code.

    Hope this helps,
    Jeroen

IMN logo majestic logo threadwatch logo seochat tools logo