Robert, that works!!! I have another posting that basically needs to
do the same thing, but will use a single checkbox. Can you help???
OK, the coding is vary similiar.
The code fragment accesses a true/false variable:
document.forms["myForm"].elements["receiveVia"].checked
Robert
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Radio buttons</title>
<style type="text/css">
..formStyle {position:relative;}
</style>
<script type="text/javascript">
function validate()
{
var x = document.forms["myForm"];
var varName = x.theName.value;
var varEmail = x.theEmail.value;
var varAddress = x.theAddress.value;
alert(
" varName = " + varName +
" varEmail = " + varEmail +
" varAddress = " + varAddress);
// I changed submitOK to a boolean variable.
var submitOK = true;
// Validate email: name and email
if(x.elements["receiveVia"].checked)
{
alert("verifying email fields.");
if (varName == '')
{
alert("Please fill in your Name");
submitOK = false;
}
if (varEmail == '')
{
alert("Please fill in Email");
submitOK = false;
}
if (varAddress != '')
{
alert("Please erase the address field.");
submitOK = false;
}
return submitOK;
}
// Validate print: name, email, and address
else
{
alert("Verifying printed fields");
// Error message should be in the order on the form
if (varName.length == '')
{
alert("Please fill in your Name");
submitOK = false;
}
if (varEmail == '')
{
alert("Please fill in Email");
submitOK = false;
}
if (varAddress == '')
{
alert("You must enter your Address");
submitOK = false;
}
return submitOK;
}
}
function showHidden(doWhat)
{
// Check if the getElementById method is available
if (document.getElementById)
{
document.getElementById("field3").style.display = doWhat;
}
else if (document.all)
{
alert("Running an older version of IE.");
document.all.field3.style.display = doWhat;
}
else
{ alert("Cannot change visibility of address field"); }
}
function decideDisplay()
{
var x = document.forms["myForm"];
if (x.receiveVia.checked == true)
{ showHidden('none');
document.forms['myForm'].theAddress.value = '';
}
else
{ showHidden('');}
}
</script>
</head>
<body>
<p>Please try out our form.</p>
<form name="myForm"
action="
http://www.nonamedomain.com"
method="POST"
onsubmit="alert('submitting');return validate();">
<p><input type="checkbox"
name="receiveVia"
value=""
onclick="decideDisplay();"> Via
email</p>
<p>Name:<br>
<input type="text" name="theName" size="20"><br><br>
Email:<br>
<input type="text" name="theEmail" size="20"><br>
<script type="text/javascript">
// Only insert a div if we can change it
if (document.getElementById || document.all)
{ document.write("<div id='field3' class='formStyle'>");}
</script>
Postal address:<br>
<input type="text" name="theAddress" size="40">
<script type="text/javascript">
if (document.getElementById || document.all)
{ document.write("</div>");}
</script>
</p>
<p><input type="submit"
border="0"
value="Submit form"
width="75"
height="17"
ALT="Submit button"></p>
</form>
<script type="text/javascript">
// In the case of a reload, the radio button may already be clicked.
// This code needs to be after the form.
decideDisplay();
</script>
</body>
</html>