javascript radio button

validating in forms

Page 1 of 1

4 Replies - 13734 Views - Last Post: 27 October 2010 - 08:13 PM

#1 debeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-July 08

javascript radio button

Posted 25 October 2010 - 09:23 AM

I am stuck as how how procedd in validating my radio buttons,
I have 5 buttons and am writing a fucntion but am losing my direction ?

<!-- radio button validation-->
			<script type="text/javascript">
			     function checkradio(value)
				 {
				 if (document.forms[0].elements[0].checked ==true);
				    { document.forms[0].elements[1].checked =false
					document.forms[0].elements[2].checked =false
					document.forms[0].elements[3].checked =false
					document.forms[0].elements[4].checked =false
					}//end if 
					else
				 }

do i have to do tis for each button...??
				 
				 
			
			
			
			
			
		</script><!--ends the javascript-->



Is This A Good Question/Topic? 0
  • +

Replies To: javascript radio button

#2 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: javascript radio button

Posted 25 October 2010 - 01:53 PM

You definitely can do it that way, but you can do it another way if you want. It depends on whether this is an assignment or not and on the structure of your code and on the rules of the validation.
Was This Post Helpful? 0
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: javascript radio button

Posted 25 October 2010 - 05:26 PM

<!-- radio button validation-->
<script type="text/javascript">
function checkradio(value)
{
    if (document.forms[0].elements[0].checked ==true); // this ends your if statement
    { 
        document.forms[0].elements[1].checked =false
	document.forms[0].elements[2].checked =false
	document.forms[0].elements[3].checked =false
	document.forms[0].elements[4].checked =false
    }//end if 
    else
    {

    }
}

do i have to do tis for each button...??
				 
				 
			
			
			
			
			
		</script><!--ends the javascript-->





This post has been edited by Nakor: 25 October 2010 - 05:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4136
  • View blog
  • Posts: 13,051
  • Joined: 08-June 10

Re: javascript radio button

Posted 25 October 2010 - 11:13 PM

you could try a loop
Was This Post Helpful? 0
  • +
  • -

#5 JMRKER  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 134
  • View blog
  • Posts: 868
  • Joined: 25-October 08

Re: javascript radio button

Posted 27 October 2010 - 08:13 PM

Or you can use a function ...
function getRBtnName(GrpName) {
  var sel = document.getElementsByName(GrpName);
  var fnd = -1;
  var str = '';
  for (var i=0; i<sel.length; i++) {
    if (sel[i].checked == true) { str = sel[i].value;  fnd = i; }
  }
//  return fnd;   // return option index of selection
// comment out next line if option index used in line above  
  return str;
} 


Returns string value of radio button (if selected) or -1 if nothing selected.
Can also return index of radio button rather than the string value.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1