SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validating which radio button checked - need help.

    Hello All

    I'm trying to validate 2 fields on the condition that a radio button has already been checked to yes.

    Basically these fields only appear if the radio button is checked so my validation for these fields should only run if the radio button is checked.

    Thing is I can't get the script to detect the radio button value working.

    Hers' the JavaScript, it form's part of a larger Validation script which I have cut down I know the script works in general as other fields validate fine.

    Code:
    function validate_form ( )
    {
    	valid = true;
    if ( document.propertyform.anyothers.value == "Yes" )
            {
    			MY NESTED VALIDATION SCRIPT WILL GO IN HERE
            }
    		
            return valid;
    }
    Here's a snippet of my HTML that the JavaScript is validating:

    Code:
    <table width="100%"  border="0" cellspacing="2">
    	  <tr>
    		<td rowspan="2" valign="top">Any others:</td>
    		<td align="right" width="30">Yes </td>
    		<td align="right" width="30">No </td>
    	</tr>
    	<tr>					
    		<td align="right"><input type="radio" value="Yes" name="anyothers" <?php if($_SESSION['sessanyothers'] == "Yes") echo 'checked'; ?> onclick="showTextBox(this)" >
    		</td>
    	        <td align="right"><input type="radio" value="No" name="anyothers" <?php if($_SESSION['sessanyothers'] == "No") echo 'checked'; ?> onclick="showTextBox(this)" >
    		</td>
    	 </tr>						
    </table>
    I hope someone can help.

    Cheers

    Paul

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's a common newbie misconception. Radio buttons do not act like <select> elements when you're trying to access them via JavaScript/DOM.

    You need to loop through the radio buttons and check which one has its checked property set to true.

    Code:
    var rads = document.propertyform.anyothers;
    for(var i=0;i<rads.length;i++) {
      if(rads[i].checked) alert(rads[i].value);
    }
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice one, cheers Kravvitz.

  4. #4
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Kravvitz (all)

    I'm trying to implement this but can't work out how to pass the result of your code to my validation script, being a noob - you may have noticed I don't use JavaScript very often.

    This is where I've got to (it's not working), trying to create a variable with a value of the checked radio button that I can then use in my validation IF statement.

    BTW all other validation in the script (which I've stripped) is still working so at least I haven't broken anything.

    HTML Code:
    function validate_form ( )
    {
    
    var rads = document.propertyform.anyothers;
      for(var i=0;i<rads.length;i++) 
      {
        if(rads[i].checked) 
        {
            var others = (rads[i].value);
        }
      }
    	valid = true;
    
    if (others == "Yes" )
            {
    			RUN MY SUB-VALIDATION SCRIPTS
            }
    		
            return valid;
    }
    Thanks for the help you've already provided Kravvitz.

    Kravvitz and anyone, any ideas?

    Cheers

    Paul

  5. #5
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome

    Try this. (The break statement is used to terminate the loop early when it's not needed any more.)
    Code:
      var others='';
      for(var i=0;i<rads.length;i++) 
      {
        if(rads[i].checked) 
        {
          others = (rads[i].value);
          break;
        }
      }
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


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
  •