n00bfest
http://n00bfest.com/phpBB3/

Need some help with some Javascript
http://n00bfest.com/phpBB3/viewtopic.php?f=11&t=45993
Page 1 of 1

Author:  Jason0885 [ Thu Aug 23, 2012 7:36 pm ]
Post subject:  Need some help with some Javascript

Me arrays and for loops do not get along.


In the below code. I have the two radio dots.

This section will display the number 2 for the length, and will check if the 1st radio dot is checked, verify it is true, and display the value yes.

However, the second dot returns a false. It goes to the else statement, just like it does when I leave the radio dots unchecked. .

I can get this to do a vice versa, or have them all set to false or null.

for (z=0; z<document.trip.radPermission.length; z++)
If I add a [z] they always show a null value.

For alert(document.trip.radPermission.length); it displays 2, if I add a [z] it displays undefined

For if (document.trip.radPermission[z].checked === true) if I remove the [z] it does not check the array at all and pulls a undefined as the value. With the z it only checks the 1st position in the array.

Even if I set the default state to checked, the no form still comes back as false.

I've also noticed the value is always yes.


Here is the part that is giving me problems, ignore all the alerts im using them to determine what value(s) it shows where.

I have also tried without the Permission variable and only used document.trip.radPermission to determine if that was the problem.


Code:
for (i=0; i<document.trip.radPermission.length; i++)
            {
                //alert(document.trip.radPermission.length);
                //alert(document.trip.radPermission[i].value);
               //alert(document.trip.radPermission[i].checked);
               
                Permission = document.trip.radPermission[i].checked;
               
        if  (Permission !== false)
                {
                        //alert(document.trip.radPermission[i].value);
                        //alert(document.trip.radPermission[i].checked);
                    return true;
                        }     
                else
                    {
                        //alert(document.trip.radPermission[i].value);
                        //alert(document.trip.radPermission[i].checked);
                    alert("Please indicate if you do or do not have permission to go.");
                    return false;
                }
                       
            }


Code:
<html>
<head>
<title>Field Trip Consent Form</title>

<script type="text/javascript">

function validateForm() {
    var Destination = document.trip.destination.value;
    var Month = document.trip.month.value;
    var Day = document.trip.day.value;
    var Year = document.trip.year.value;
        var i;
        var z;
        var Permission;
       
        if (Destination==null || Destination=="")
           {
           alert("A Destination must entered.");
           return false;
           }
    if (Month === "Month")
           {
           alert("A Month must be selected.");
           return false;
           }
    if (Day === "Day")
            {
            alert("A Day must be selected.");
            return false;
                }
       
        for (i=0; i<document.trip.txtParent.length; i++)
            {
       
            if (document.trip.txtParent[i].value === null || document.trip.txtParent[i].value==="")
                {
                alert("Your Parent's information must be entered.");
                return false;
                }
                }
               
        for (i=0; i<document.trip.radPermission.length; i++)
            {
                //alert(document.trip.radPermission.length);
                //alert(document.trip.radPermission[i].value);
            //alert(document.trip.radPermission[i].checked);
               
                Permission = document.trip.radPermission[i].checked;
               
        if  (Permission !== false)
                {
                        //alert(document.trip.radPermission[i].value);
                        //alert(document.trip.radPermission[i].checked);
                    return true;
                        }     
                else
                    {
                        //alert(document.trip.radPermission[i].value);
                        //alert(document.trip.radPermission[i].checked);
                    alert("Please indicate if you do or do not have permission to go.");
                    return false;
                }
                       
            }
}
</script>
</head>
<body>
<h1>Field Trip Consent Form</h1>

<form name="trip" onsubmit="return validateForm()">

<h2>Description of Trip</h2>
<p>Destination &nbsp;
<input type="text" name="destination" SIZE="50" /></p>

<p>Date of Trip &nbsp;
<select name="month">
<option selected value="Month">Month
<option value="Jan">Jan
<option value="Feb">Feb
<option value="Mar">Mar
<option value="Apr">Apr
<option value="May">May
<option value="Jun">Jun
<option value="Jul">Jul
<option value="Aug">Aug
<option value="Sep">Sep
<option value="Oct">Oct
<option value="Nov">Nov
<option value="Dec">Dec
</select>

<select name="day">
<option selected value="Day">Day
<option value="01">01
<option value="02">02
<option value="03">03
<option value="04">04
<option value="05">05
<option value="06">06
<option value="07">07
<option value="08">08
<option value="09">09
<option value="10">10
<option value="11">11
<option value="12">12
<option value="13">13
<option value="14">14
<option value="15">15
<option value="16">16
<option value="17">17
<option value="18">18
<option value="19">19
<option value="20">20
<option value="21">21
<option value="22">22
<option value="23">23
<option value="24">24
<option value="25">25
<option value="26">26
<option value="27">27
<option value="28">28
<option value="29">29
<option value="30">30
<option value="31">31
</select>

<select name="year">
<option selected value="2012">2012
<option value="2012">2012
<option value="2013">2013
<option value="2014">2014
<option value="2015">2015
</select>


<h2>Parental Information</h2>
<p>Mother's Name &nbsp;
   <input type="text" name="txtParent" size="20" />
</p>
<p>Mother's Work Phone &nbsp;
   <input type="text" name="txtParent" size="20" />
</p>
<p>Father's Name &nbsp;
   <input type="text" name="txtParent" size="20" />
</p>
<p>Father's Work Phone &nbsp;
   <input type="text" name="txtParent" size="20" />
</p>
<p><input type="radio" name="radPermission" value="Yes"/> Permission is Granted &nbsp;
   <input type="radio" name="radPermission" value="no"/> Permission is NOT Granted &nbsp;
</p>

<br>
<p><input type="submit" value="Submit This Data" /> &nbsp; &nbsp;
   <input type="reset" /></p>
<br />

</form>
</body>
</html>

Author:  Jason0885 [ Thu Aug 23, 2012 7:38 pm ]
Post subject:  Re: Need some help with some Javascript

Also, it has to use Arrays, For Loops, and if else statement. This is for a class.

Author:  Shredder [ Fri Aug 24, 2012 12:12 pm ]
Post subject:  Re: Need some help with some Javascript

I am assuming you are testing to see if any box is unchecked.

You are iterating the array so I think you are trying to verify all checkbox values in the same loop, but the loop will exit on the first value it tests, so you are only ever looking at the first object in the array.

Since this is a validation if you want to fail on only false values then the test loop needs to ignore true values.

change the test logic to read:

if (!document.trip.radPermission[i].checked)
return false;

and after the loop ends:

return true;

This will fail the validation on any unchecked box.

Author:  Jason0885 [ Fri Aug 24, 2012 1:10 pm ]
Post subject:  Re: Need some help with some Javascript

Thanks Shredder, that got it working..

The next thing I get to play with is json and Ajax..

Author:  Gman [ Mon Aug 27, 2012 12:19 pm ]
Post subject:  Re: Need some help with some Javascript

I'd be willing to guess that if anyone here is good w/ JSON and Ajax, it's Shredder.

Author:  Jason0885 [ Mon Aug 27, 2012 12:47 pm ]
Post subject:  Re: Need some help with some Javascript

Gman wrote:
I'd be willing to guess that if anyone here is good w/ JSON and Ajax, it's Shredder.


I concur, but I'd rather work with it for a week or two before I seek advice. Fun stuff, so far, fun stuff.

The best part about this is Shredder's advice was easier to understand than my teachers, lol.

Author:  Reborn [ Mon Aug 27, 2012 2:02 pm ]
Post subject:  Re: Need some help with some Javascript

Real world knowledge & experience always > those from a purely academic standpoint :) I've also observed that most of the time, having someone other than the Professor explain things will help clear things up greatly.

I've also played around with JSON and Ajax. If you're allowed to use JQuery, it's very easy (and beautiful). Now I can't live without it.

Author:  Jason0885 [ Mon Aug 27, 2012 3:10 pm ]
Post subject:  Re: Need some help with some Javascript

Well this particular project I am working on, does not use JQuery. I should say I'm not suppose to. It actually uses a flaw with IE 9 to work from my understanding. The teacher is actually thinking about changing the assignment because of that. (I am actually 6 projects ahead of the class schedule) He stated I can go ahead and do this one, or wait until he changes it. I'm try hard when it comes to school so I'll work on it if not just for fun.

Page 1 of 1 All times are UTC - 6 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/