www.webdeveloper.com
Results 1 to 6 of 6

Thread: Nesting an if statement with a loop

  1. #1
    Join Date
    Dec 2011
    Posts
    5

    Nesting an if statement with a loop

    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

    <!--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>
    Thank you
    Deanno007

  2. #2
    Join Date
    Nov 2007
    Posts
    409
    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.

  3. #3
    Join Date
    Dec 2011
    Posts
    5

    Thank you.

    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

  4. #4
    Join Date
    Dec 2011
    Posts
    5
    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>&nbsp;</td></tr>
    <tr valign="top"><td colspan="2" style="font-family:verdana;color:green;"><b>Home Team</b></td>
    <td>&nbsp;</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">&nbsp;</td><td width="175">&nbsp;</td><td>&nbsp;</td></tr>
    </table>
    <input name="Submit" type="submit" tabindex="50" value="Send" onclick ="Validate()"> &nbsp;&nbsp;&nbsp;
    <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>

  5. #5
    Join Date
    Nov 2007
    Posts
    409
    Here's a sample that shows how to do it.
    I've included my "getCheckedvalue" function that takes the name of the radio button as input.

    Code:
    <script>
    function validate(){
    	for(z=1;z<5;z++){
    		if (getCheckedValue('game'+z) == '')
    			{alert('no winner is selected for game '+z); return false;}
    	}
    	alert('Your tips are being forwarded to ****.\r\n thank you for your attention. And good luck!');
    	return true;
    }
    
    function getCheckedValue(radioName) {
        var radioObj = document.getElementsByName(radioName);
        if(!radioObj)
            return "";
        var radioLength = radioObj.length;
        if(radioLength == undefined)
            if(radioObj.checked)
                return radioObj.value;
            else
                return "";
        for(var i = 0; i < radioLength; i++) {
            if(radioObj[i].checked) {
                return radioObj[i].value;
            }
        }
        return '';
    }
    </script>
    <form name="myForm" action="">
    1 Yes: <input type=radio name=game1 value="yes"><br/>
    1 No: <input type=radio name=game1 value="no"><br/>
    2 Yes: <input type=radio name=game2 value="yes"><br/>
    2 No: <input type=radio name=game2 value="no"><br/>
    3 Yes: <input type=radio name=game3 value="yes"><br/>
    3 No: <input type=radio name=game3 value="no"><br/>
    4 Yes: <input type=radio name=game4 value="yes"><br/>
    4 No: <input type=radio name=game4 value="no"><br/>
    <a href="javascript:void(0);" onclick="validate()">Validate</a>
    </form>

  6. #6
    Join Date
    Dec 2011
    Posts
    5

    Smile Nesting an if statement inot loop

    Thank you very much napOLean
    your explantion is excellent and very helpful.
    I will now push on.
    Deanno007

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles