www.webdeveloper.com
Results 1 to 6 of 6

Thread: Validate dynamically named checkboxes

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Posts
    10

    Validate dynamically named checkboxes

    Hi All, I'm trying to validate that at least 1 checkbox is checked on a page: This is my code for the checkboxes:

    <cfloop query="getprojects">
    <tr>
    <td bgcolor="##CCCCCC"><input type="checkbox" name="f_project_#project_name_id#" value="#project_name_id#" id="f_project_#project_name_id#" onClick="javascript:enableField_#project_name_id#()">#project_name#</td>
    <td bgcolor="##CCCCCC"><input name="f_support_#project_name_id#" type="radio" value="1" id="f_support_#project_name_id#" disabled></td>
    <td bgcolor="##CCCCCC"><input name="f_support_#project_name_id#" type="radio" value="0" id="f_support_#project_name_id#" disabled></td>
    <td bgcolor="##CCCCCC"><input name="f_support_#project_name_id#" type="radio" value="2" id="f_support_#project_name_id#" checked disabled></td>
    </tr>

    </cfloop>

    I have the following javascript to try to validate:

    script language="JavaScript" type="text/JavaScript">
    function validate() {
    //check a field to make sure it has something filled in

    count = 0;

    <cfloop query="getprojects">
    for(x=0; x<document.stakeholder.f_project_<cfoutput>#project_name_id#</cfoutput>.length; x++){
    if(document.stakeholder.f_project_<cfoutput>#project_name_id#</cfoutput>[x].checked==true){
    count++
    }
    }
    </cfloop>

    if(count==0){
    alert("Select a project");
    return false;
    }


    </script>

    Can anyone help me out????????

  2. #2
    Join Date
    Nov 2010
    Posts
    1,083
    when yousay "checkboxes" do you include the radio buttons... because you have one hardcoded as "checked" there, so it becomes a little meaningless. But anyway, maybe this is a start...

    Code:
    <body>
    <table>
    <tr id="boxes">
    <td bgcolor="##CCCCCC"><input type="checkbox" name="f_project_#project_name_id#" value="#project_name_id#" id="f_project_#project_name_id#" onClick="javascript:enableField_#project_name_id#()">#project_name#</td>
    <td bgcolor="##CCCCCC"><input name="f_support_#project_name_id#" type="radio" value="1" id="f_support_#project_name_id#" disabled></td>
    <td bgcolor="##CCCCCC"><input name="f_support_#project_name_id#" type="radio" value="0" id="f_support_#project_name_id#" disabled></td>
    <td bgcolor="##CCCCCC"><input name="f_support_#project_name_id#" type="radio" value="2" id="f_support_#project_name_id#" disabled></td>
    </tr>
    </table>
    <input type="button" value="validate" onclick="valBoxes()"/>
    <script>
    inps=document.getElementById("boxes").getElementsByTagName("input");
    function valBoxes() {
    var count=0;
    for (var i = 0; i < inps.length; i++) {
    if(inps[i].checked){
    count++
    			}
    		}
    if(count==0){
    alert("Select a project");
    return false;
    			}	
    		}
    
    </script>
    </body>

  3. #3
    Join Date
    Feb 2012
    Posts
    10

    no...

    No.....radio buttons are not included.....I'm trying out your code...

  4. #4
    Join Date
    Nov 2010
    Posts
    1,083
    in that case if you are just looking at checkboxes it would make more sense to do this...

    Code:
    <script>
    inps=document.getElementsByTagName("input");
    function valBoxes() {
    var count=0;
    for (var i = 0; i < inps.length; i++) {
    if(inps[i].checked&&inps[i].type=="checkbox"){
    count++
    			}
    		}
    if(count==0){
    alert("Select a project");
    return false;
    			}	
    		}
    
    </script>
    of course if you have other (unrelated) checkboxes you can further refine the test by giving the target cb's a class and checking for className in the function

  5. #5
    Join Date
    Feb 2012
    Posts
    10

    Not sure what is up...

    but it will still let me submit my form without a checkbox being checked. I have attached my entire script for validation:

    Code:
    <script language="JavaScript" type="text/JavaScript">
    function validate() {
    //check a field to make sure it has something filled in
    inps=document.getElementsByTagName("input");
    var count=0;
    for (var i = 0; i < inps.length; i++) {
    if(inps[i].checked&&inps[i].type=="checkbox"){
    count++
    			}
    		}
    if(count==0){
    alert("Select a project");
    return false;
    			}	
    				
    if(document.stakeholder.f_type_of_stakeholder.value == "0")
    { alert ( "Please select a Type of Stakeholder." ); return false; } 
    document.stakeholder.f_type_of_stakeholder.focus();
    
    
    var bus=document.stakeholder.f_type_of_stakeholder.value;
    if(bus=="5")
        {
        var txt=document.stakeholder.f_organ.value;
            if(txt=="")
            {
             alert("Organization Name is required");
            return false;
            }
    	return true 	
        }
    
    	
    if(document.stakeholder.f_contact_date.value == "") {
    alert("Please select a date.");
    document.stakeholder.f_contact_date.focus();
    return false;
    }
    
    if(document.stakeholder.f_fname.value == "") {
    alert("Please enter a First Name.");
    document.stakeholder.f_fname.focus();
    return false;
    }
    
    if(document.stakeholder.f_lname.value == "") {
    alert("Please enter a Last Name.");
    document.stakeholder.f_lname.focus();
    return false;
    }
    
    return true;
    } 
    </script>
    the ones for first name and last name work if they are blank....

  6. #6
    Join Date
    Feb 2012
    Posts
    10

    I got it...

    Hey, i got it, I had some other validation script stuff for some other fields and it appears that script was messing this up.....anyway, THANK YOU, your code worked perfect!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles