I am having trouble getting this code to work. Can anybody help please?
function validate()
{
//validate the select name box
if (document.tipsform.Name.selectedIndex ===0)
{ alert('You have left out your name');return false;}
//validate the radio buttons
for(z=1;z<4;z++)
if (!document.tipsform.gamez[0].checked && !document.tipsform.gamez[1].checked && !document.tipsform.gamez[2].checked)
{alert('no winner is selected for game z'); return false;}
else
{alert('Your tips are being forwarded to ****.\r\n thank you for your attention. And good luck!'); return true;}
}
</script>
now this is supposed to link to the next code and validate the checking of radio buttons x 3 sets - hence the loop variance of 1-4
Your For loop.
Looks like you forgot the {} that tell it what to loop.
The code you posted does not include the form tags, etc., so I can't give it a robust test.
Code:
for(z=1;z<4;z++) {
if (!document.tipsform.gamez[0].checked && !document.tipsform.gamez[1].checked && !document.tipsform.gamez[2].checked)
{alert('no winner is selected for game z'); return false;}
else
{alert('Your tips are being forwarded to ****.\r\n thank you for your attention. And good luck!'); return true;}
}
In your alerts, if you want the game numbers printed in the alert, you need to close your quotes and append the z variable to the line, like this:
Code:
alert('no winner is selected for game ' + z);
If you'll post code that can be saved as an HTML file that actually works, we can help more.
solution sounds great & makes sense.
However The greatest problem is gamez where z is the variable and the name of the radio button is game1,game2 etc and will not be separated, if coded as game 1 it doesnt work.
Thanks Deanno007
napOleon, Thank you,
Please find following the complete form coded with some personal details removed.
This code is supposed to check the form for completion and alert the user if a field is not complete
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Javascript trial </title>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function validate()
{
//validate the select name box
if (document.tipsform.Name.selectedIndex ===0)
{ alert('You have left out your name');return false;}
//validate the radio buttons
for(z=1;z<4;z++){
if (!document.tipsform.gamez[0].checked && !document.tipsform.gamez[1].checked && !document.tipsform.gamez[2].checked)
{alert('no winner is selected for game'+z); return false;}}
else
{alert('Your tips are being forwarded to ****.\r\n thank you for your attention. And good luck!'); return true;}
}
</script>
</head>
<body bgcolor="beige">
<center>
<!--Introducing form and action on submit-->
<form name="tipsform" id="tipsform" method="post" action="http://www.*****.com./FormMailer" onSubmit="return validate();">
<input name="to" type="hidden" id="to" value="joe@hotmail.com">
<input name="from" type="hidden" id="from" value="tips@Joe.com">
<input name="subject" type="hidden" id="subject" value="Week 1 2012">
<table border="0" cellpadding="0">
<!--Heading across top of page-->
<tr><td colspan="1" align="right"></td><td colspan="4" style="font-family:Verdana; font-size:xx-large; colorarkred" align="center"><u> Javascript trial</u></td>
<td align="left"></td></tr>
<!--instructions for users -->
<tr><td colspan="8" style="font-family:tahoma;font-size:12;color:blue; align:center">
<br><br>Please make sure your internet browser has the option for javascript turned on ! ! ! !</td></tr>
<tr><td colspan="8" style="font-family:fantasy; font-size:xx-large; color:Maroon" align="center">
<br>Your tips are due this week, by Friday February 17<sup>th</sup> at 1930Hrs (7:30 PM)<br><br></td></tr>
<!--Input for Identifying data -->
<tr><td style="font-family:comic sans MS; color:green; font-size:16 "align="right" colspan="2">Selections for</td><td></td>
<td><select name="Name" id="Name" size="1">
<option selected> 0 </option> <option>*</option><option>**</option><option>***</option><option>****</option><option>?</option>
<option>P</option><option>Q</option></select></td><td width="145"></td></tr>
<tr><td height="20"></td></tr>
<tr><td></td></tr>
<tr><td colspan="9" style="font-family:Comic sans MS;font-size:x-large; color:firebrick" align="center">
Please select your name, then one button for every match - then click send </td></tr>
<!-- Weekly header -->
<tr><td> </td></tr>
<tr valign="top"><td colspan="2" style="font-family:verdana;color:green;"><b>Home Team</b></td>
<td> </td><td colspan="2" align="center" style="font-family:verdana;color:green;"><b>Away Team</b></td>
<td></td><td></td><td></td><td align="right" style="font-family:verdana;color:green;"><b>Venue</b></td></tr>
<!-- Places line between match sequences-->
<tr ><td height="20" colspan="9" align="justify" ><hr></td></tr>
<!--Identifies the Date & time -->
<tr><td colspan="2" style="font-family:sans-serif; font-size:large; color:Chocolate">Friday 17th Feb 2012 </td><td></td></tr>
<!--Identifies the games -->
<!--game 1-->
<tr ><td height="45"style="font-family:verdana;color:green;" align="right"><input type="radio" name="game1" value=
"H"></td><td align="left" style="font-family:verdana;color:green;">
Hawthorn</td><td align="left"><font color="#FF0000">Vs</font></td><td align="right"><input type="radio" name="game1" value=
"R"></td><td align="left" style="font-family:verdana;color:green;">
Richmond</td><td align="right"><input type="radio" name="game1" value="="></td>
<td align="left" style="font-family:verdana;color:green;">Draw</td><td></td><td align="right" style="font-family:verdana;color:green;">
E S</td><td width="140">.</td></tr>
<tr><td height="15"></td></tr>
<!-- Game 2-->
<tr ><td height="45" style="font-family:verdana;color:green;" align="right"><input type="radio" name="2" value=
"R"></td><td align="left" style="font-family:verdana;color:green;">
Richmond</td><td align="left"><font color="#FF0000">Vs</font></td><td align="right"><input type="radio" name="2" value=
"N"></td><td align="left" style="font-family:verdana;color:green;">
North Melbourne</td><td align="right"><input type="radio" name="2" value="="></td>
<td align="left" style="font-family:verdana;color:green;">Draw</td><td></td><td align="right" style="font-family:verdana;color:green;">
E S</td></tr><tr><td height="15"></td></tr>
<!--Game 3-->
<tr ><td height="45" style="font-family:verdana;color:green;" align="right"><input type="radio" name="3" value=
"H"></td><td align="left" style="font-family:verdana;color:green;">
Hawthorn</td><td align="left"><font color="#FF0000">Vs</font></td><td align="right"><input type="radio" name="3" value=
"N"></td><td align="left" style="font-family:verdana;color:green;">
North Melbourne</td><td align="right"><input type="radio" name="3" value="="></td>
<td align="left" style="font-family:verdana;color:green;">Draw</td><td></td><td align="right" style="font-family:verdana;color:green;">
E S</td></tr><tr><td height="15"></td></tr>
<!-- Places line between match sequences-->
<tr><td height="20" colspan="9" align="justify" ><hr></td></tr>
</table>
<!-- end of game details, start of function buttons-->
<table border="0" cellpadding="0" width="550" id="table3">
<tr><td width="175"> </td><td width="175"> </td><td> </td></tr>
</table>
<input name="Submit" type="submit" tabindex="50" value="Send" onclick ="Validate()">
<input type="reset" value="Reset" name="resetButton" tabindex="50">
</form>
<!-- end of form, instructions for new users-->
<p style="font-family:Verdana;color:green; height:145">If you have questions or difficulties with this page,<br>
please ring ++++++ on 04***********<br><br>or use <a href="mailto:****@hotmail.com?Subject=Please%20help%20me">
please help me</a> link<br><br> to email **** for further instructions on how to complete</p>
</center>
</body>
</html>
Bookmarks