Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Valid only when radio select

Status
Not open for further replies.

deuxk

Programmer
Jun 2, 2007
3
0
0
CA
Hi,

i try to make web page with javascript. It's work, but i make validation and i want start the validation only when radio (changement) is checked.

the page :

<html>
<head>
<title>Paiement</title>
<script type="text/javascript">
function HideShow(controlToHide)
{
if (document.getElementById)
{
// Hide all regions
document.getElementById('txtShow1').style.display = 'none';
document.getElementById('txtShow2').style.display = 'none';
document.getElementById('txtShow1').disabled = 'disabled';
document.getElementById('txtShow2').disabled = 'disabled';
// Display the requested region
document.getElementById
('txtShow' + controlToHide).style.display = 'block';
document.getElementById
('txtShow' + controlToHide).disabled = '';
}
else
{
alert('Un erreur est survenu...');
}
}

</script>

<script language="JavaScript">
function Validation(theForm) {
if (theForm.radio.value == "payer") { return (true); }

if (theForm.adresse_input.value == "") {
alert("l'adresse est obligatoire");
theForm.adresse_input.focus();
return (false);
}
if (theForm.pays.value == "") {
alert("vous devez choisir un pays");
theForm.pays.focus();
return (false);
}
if (theForm.province.value == "") {
alert("vous devez choisir une province");
theForm.pays.focus();
return (false);
}
if (theForm.ville.value == "") {
alert("vous devez entrer une ville");
theForm.ville.focus();
return (false);
}
if (theForm.codepostal.value == "") {
alert("vous devez entrer un code postal");
theForm.ville.focus();
return (false);
}

return (true);
}
</script>
</head>
<body>

<?php
require_once('modules/cart/mysql.php');
require_once('modules/cart/global.php');
require_once('modules/cart/functions.php');

echo showpaie();
?>
<form name="frmTest" id="frmTest" action="site.php?cmd=achat" method="post" onSubmit="return Validation(this)">
<table cellpadding="0" cellspacing="0">

<tr valign="top">
<td colspan="2" bgcolor="#000000"><strong style="color:white">Adresse de l'envoie</strong></td>
</tr>

<tr>
<td valign="top">
<input name="radio" type="radio" value="payer" onclick="HideShow(1)" checked /><br />
</td>
<td>
<?php include("envoie_tableau.php"); ?>
</td>

</tr>

<tr>
<td colspan="3" align="right">
<input type="submit" value="Payer" id="txtShow1"name="txtShow1" style="display:block;" /><br />
</td>
</tr>

<tr>
<td colspan="2" bgcolor="#000000"><strong style="color:white">Changer l'adresse de reception</strong></td>
</tr>
<tr>
<td valign="top">
<input name="radio" type="radio" value="changement" onclick="HideShow(2)"/><br />
</td>
<td>
<?php include("changement_tableau.php"); ?>
</td>
</tr>

<tr>
<td colspan="3" align="right">
<input type="submit" value="Changer" id="txtShow2"name="txtShow2" style="display:none;" /><br />
</td>
</tr>
</table>
</form>
</body>
</html>
 
The group of radio buttons, referred to as theForm.radio, is an array of elements. Each one always has a value. You must test the checked property instead of the value.
Code:
...
if ( theForm.radio[0].checked )  { return (true); }
...


Extra advice. Dont use keywords as the name of things. It will just lead to confusion, both for you and sometimes the computer. Give your radio buttons a meaningful name like scenario, or pay_or_change. Something that tells the next person why these radio buttons belong together.

 
A more robust approach would use ID attributes in the radio buttons. This will allow you to re-arrange the order of the buttons without revising the validation code.
Code:
<!-- In the HTML -->
...
<input name="scenario" type="radio" value="changement" 
 id="changing" 
onclick="HideShow(2)"/>
...
<input name="scenario" type="radio" value="payer" 
 id="paying" 
onclick="HideShow(1)" checked />

...


//In the validation function 
...
var btnChangement = document.getElementById("changing");
if ( btnChangement.checked ) { return (true); }
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top