Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post

    Validating radio[] array problem

    hi all,
    i have radio buttons for every group on my PHP page which populate dynamically from the DB:

    Yes<INPUT TYPE="radio" NAME="rollback_<?=$arr['Audit_ID']?>" id="RollBackRadio[]" value="1"><br>
    No<INPUT TYPE="radio" NAME="rollback_<?=$arr['Audit_ID']?>" id="RollBackRadio[]" value="0">

    <script language="javascript">
    function checkRadio(){
    var radios = document.mainform.elements["RollBackRadio[]"];
    var radflag=false;
    for (var i=0; i<radios.length; i++){
    if (radios[i].checked){
    radflag=true;
    } else {
    radflag=false;
    }
    }
    return radflag;
    }

    </script>
    for eg ... if there are three audits on the page then there would be three sets of radio buttons. how do i validate that atleast one radio is checked for every Audit
    Note : i cannot change the name of the radio button. probably would have to play with IDs. i tried giving id to the radio but the script is getting length as 6 [for the above case where audits are 3] instead of 3 ..
    how can i do it??
    Thanks in advance.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    if u can't change the name then change the id

    Code:
    Yes<INPUT TYPE="radio" NAME="rollback_1" id="RollBackRadio0_1" value="1"><br>
    No<INPUT TYPE="radio" NAME="rollback_1" id="RollBackRadio0_0" value="0">
    and check if anything is selected by using the group number of the radio in this case 0

    Code:
    function checkRadio(group){
    	var cnt = 0;
    	var el = document.getElementById('RollBackRadio' + group + '_' + cnt);
    	var flag = false;
    	while ( !!el && !flag ) {
    		flag = el.checked;
    		cnt++;
    		el = document.getElementById('RollBackRadio' + group + '_' + cnt);
    	} // endwhile
    	alert( (flag ? 'something' : 'nothing') + ' is selected');
    	return flag;
    }
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    bhakti_thakkar (11-03-2007)

  • #3
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post
    Hi Shyam,

    Thanks for the helpful reply

    Quote Originally Posted by shyam View Post
    if u can't change the name then change the id

    Code:
    Yes<INPUT TYPE="radio" NAME="rollback_1" id="RollBackRadio0_1" value="1"><br>
    No<INPUT TYPE="radio" NAME="rollback_1" id="RollBackRadio0_0" value="0">
    and check if anything is selected by using the group number of the radio in this case 0

    Code:
    function checkRadio(group){
    	var cnt = 0;
    	var el = document.getElementById('RollBackRadio' + group + '_' + cnt);
    	var flag = false;
    	while ( !!el && !flag ) {
    		flag = el.checked;
    		cnt++;
    		el = document.getElementById('RollBackRadio' + group + '_' + cnt);
    	} // endwhile
    	alert( (flag ? 'something' : 'nothing') + ' is selected');
    	return flag;
    }

  • #4
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by shyam View Post
    if u can't change the name then change the id

    Code:
    Yes<INPUT TYPE="radio" NAME="rollback_1" id="RollBackRadio0_1" value="1"><br>
    No<INPUT TYPE="radio" NAME="rollback_1" id="RollBackRadio0_0" value="0">
    and check if anything is selected by using the group number of the radio in this case 0

    Code:
    function checkRadio(group){
    	var cnt = 0;
    	var el = document.getElementById('RollBackRadio' + group + '_' + cnt);
    	var flag = false;
    	while ( !!el && !flag ) {
    		flag = el.checked;
    		cnt++;
    		el = document.getElementById('RollBackRadio' + group + '_' + cnt);
    	} // endwhile
    	alert( (flag ? 'something' : 'nothing') + ' is selected');
    	return flag;
    }
    Hey, Shyam. What does the !!el mean?

  • #5
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    well its just a fancy way of checking if a particular object/property is present...the idea is that a double negative cancel each other out so the output is always a boolean !!'', !!null, !!undefined and !!0 all return false...pretty much everything else returns true
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    BarrMan (11-03-2007)

  • #6
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    I see. That's a nice trick.

    Thanks


  •  

    Posting Permissions

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