Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: [javascript]Validating Radio buttons


Message #1 by "Alex Bienz" <alex@v...> on Mon, 8 Apr 2002 12:03:05 +0100
This is a multi-part message in MIME format.

------=_NextPart_000_00E9_01C1DF22.1155EAD0
Content-Type: text/plain;
	charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

Just check if u want this
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=3D"Content-Type" content=3D"text/html; 
charset=3Diso-8859-1">
</head>

<body bgcolor=3D"#FFFFFF" text=3D"#000000">
<script language=3D"JavaScript">
function validateme(){
for(var i=3D0;i<document.form1.myoption.length;i++){
 if(!document.form1.myoption[i].checked)
{
alert('Yes')

}
else{
alert('No')
}
}
}
</script>
<form name=3D"form1" method=3D"post" action=3D"">
  <input type=3D"radio" name=3D"myoption" value=3D"myoption">
  <input type=3D"radio" name=3D"myoption" value=3D"myoption">
  <input type=3D"radio" name=3D"myoption" value=3D"myoption">
  <input type=3D"button" name=3D"Submit" value=3D"click" 
onclick=3D"validateme();">
</form>
</body>
</html>

  ----- Original Message -----
  From: Alex Bienz
  To: javascript
  Sent: Monday, April 08, 2002 4:33 PM
  Subject: [javascript] [javascript]Validating Radio buttons


  Hi there,

  I've got a problem validating some radio buttons on my web page.  I 
have a simple form on my page with many different form object, including 
text boxes and radio buttons,

  when the submit button is clicked I run a validation script before 
submitting the page, this validation script simply checks that data has 
been input into the relevant sections and that none are left blank.

  some code for checking that a textbox has been used looks like this...

  if(b_q1 =3D=3D "") {
    alert("Please answer all questions, Q1 in BACKGROUND DETAILS is 
null.");
    return false;
   }

  This works fine, and I'm happy with it, but for my radio buttons I'm 
having more difficulty, I've found some code like this...

  function validateme(){
   if (document.form1.myoption[0].checked || 
document.form1.myoption[1].checked){alert('Yes')}
   else{alert('No')}
  }

  However I would like to reverse this, perhaps by usng a NOT somewhere, 
so that if they haven't been checked then it returns false, please bear 
in mind that I have many different radio buttons on my form so the most 
economic code would be most useful.

  Thanks in advance!

          Cheers,
                      Alex.
  --- Improve your web design skills with these new books from 
Glasshaus. Usable Web Menus 
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=3Dnosim/theprogramm
e r-20 Constructing Accessible Web Sites 
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=3Dnosim/theprogramm
e r-20 Practical JavaScript for the Usable Web 
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=3Dnosim/theprogramm
e r-20 ---



  Return to Index