ollykrem@yahoo.com (Oleg) wrote in message news:<b5d766cc.0405050657.40ebba2f@posting.google. com>...[color=blue]
> Hello there:
>
> I've been trying to create two different sets of required fields in
> one form and to use a
>
> radiobutton as sort of a switcher between these sets.
>[/color]
Here is another version of file.
The address field is only displayed when needed.
Robert
<html>
<head>
<title>Radio buttons</title>
<style type="text/css">
..PictureStyle {position:relative; visibility:hidden;}
</style>
<script type="text/javascript">
function validate()
{
// You need a var before a variable to make it local to this
// function. Without the var keyward, the variable is global.
// It is a good practice to minimize global variables.
var x = document.forms["myForm"];
// I find starting the varible with var... is confusing, but
// I left them as is. It is easy to forget to declare them.
var varCheckedButton;
// Figure out which radio button was pressed
if (x.receiveVia[0].checked == true)
{ varCheckedButton = x.receiveVia[0].value;}
else if (x.receiveVia[1].checked == true)
{ varCheckedButton = x.receiveVia[1].value;}
else
{ varCheckedButton = '';}
var varName = x.theName.value;
var varEmail = x.theEmail.value;
var varAddress = x.theAddress.value;
alert("varCheckedButton = " + varCheckedButton +
" varName = " + varName +
" varEmail = " + varEmail +
" varAddress = " + varAddress);
// I changed submitOK to a boolean variable.
var submitOK = true;
// Validate email: name and email
if (varCheckedButton == "byEmail")
{
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 if (varCheckedButton=="printed")
{
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;
}
else
{
alert("You need to select either email or print.");
return false;
}
}
function showHidden(doWhat)
{
document.getElementById("field3").style.visibility = doWhat;
}
</script>
</head>
<body>
<p>Please try out our form.</p>
<form name="myForm"
action="http://www.wsc.ma.edu/webstudents/CommWebSite/randomimages_2.pl"
method="POST"
enctype="x-www-form-urlencoded">
<p><input type="radio" name="receiveVia" value="printed"
onclick="showHidden('visible');"> Printed
brochure</p>
<p><input type="radio" name="receiveVia" value="byEmail"
onclick="showHidden('hidden');
document.forms['myForm'].theAddress.value = '';"> Via
Email</p>
<!-- I like to use component names to avoid conflicts with
reserved words. -->
<p>Name:<br>
<input type="text" name="theName" size="20"><br><br>
Email:<br>
<input type="text" name="theEmail" size="20"><br>
<div id="field3"
class="PictureStyle">
Postal address:<br>
<input type="text" name="theAddress" size="40">
</div>
</p>
<p><input type="image" src="submit.gif" border="0" value="Submit"
width="75" height="17"
ALT="Submit button" onClick="return validate();"></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.
var x = document.forms["myForm"];
if (x.receiveVia[0].checked == true)
{ showHidden('visible');}
else if (x.receiveVia[1].checked == true)
{ showHidden('hidden');}
else
{ alert("Something is amiss here.");}
</script>
</body>
</html>