PDA

View Full Version : Combo Boxes: A new Dilemma


Vagabond
03-04-2009, 04:00 AM
I figure a simple problem out, and a bigger one takes it's place. Allow me to start from the top.

I'm creating a user registration form in flash and I want to use combo boxes for the users to enter their date of birth and location.

Now, in order to protect from users from doing silly things like entering February 31st as their birthday, I'm going to populate the combo boxes through actionscript depending on the month and year the user selected.

If the user selects a month with 31 days in it, I want the date box to have 31 days in it. If they select a month with only 30 days in it, I want the date box to only have 30 days to choose from, and if the user selects February, I want only 28 days to appear, unless the user also selected a leap year.

Sounds easy enough. I fixed the problem of the boxes only populating as if the user selected February (I used = instead of == in the comparison), but now there's an even worse bug. When I run the script and try to select either a month or a year, the program crashes and tells me, "A script in this movie is causing Flash Player to run slowly. If it continues to run, your computer may become unresponsive. Do you want to abort the script?"

Given 3 combo boxes (dobMonth, dobDate, dobYear), here's the script in question:
10 // dynamic combo boxes
11 // make new date object
12 var myDate:Date = new Date();
13
14 // create dobMonth arrays
15 // if the month selected is April(4), June(6), September(9), or November(11)
16 var monthArray30:Array = new Array(4,6,9,11);
17 // if the month selected is January(1), March(3), May(5), July(7), August(8), October(10), or December(12)
18 var monthArray31:Array = new Array(1,3,5,7,8,10,12);
19
20 // this is broken... it still won't use 30 or 31
21 // create a listener for dobMonth and dobYear
22 // when these are changed, repopulate dobDate
23 var cbListener:Object = new Object();
24 cbListener.change = function(evt_obj:Object):Void {
25
26 // set monthSelected and yearSelected
27 var monthSelected:Object = dobMonth.selectedItem;
28 var yearSelected:Object = dobYear.selectedItem;
29 trace(monthSelected.data);
30 trace(yearSelected.data);
31
32 // if "monthSelected" is in the array "monthArray30" (4,6,9,11)
33 if(monthArray30.in_array(monthSelected.data)){
34
35 // set popDays to 30
36 var popDays:Number = 30;
37
38 // else, if "monthSelected" is in the array "monthArray31" (1,3,5,7,8,10,12)
39 } else if(monthArray31.in_array(monthSelected.data)){
40
41 // set popDays to 31
42 var popDays:Number = 31;
43
44 // else, if "monthSelected" is 2 (february)
45 } else if(monthSelected.data == "2"){
46
47 // determine if yearSelected is a leap year
48 // if "yearSelected" is divisible by 4, AND NOT 100, or divisible by 400
49 if(((yearSelected.data % 4 == 0) && (yearSelected.data % 100 != 0)) || (yearSelected.data % 400 == 0)){
50
51 // set popDays to 29 (leap year)
52 var popDays:Number = 29;
53
54 // else, if "yearSelected" is NOT a leap year
55 } else {
56
57 // set popDays to 28
58 var popDays:Number = 28;
59
60 } // end if((yearSelected % 4 == 0) && (yearSelected % 100 != 0)) || (yearSelected % 400 == 0)
61
62 } // end if(monthArray30.in_array(monthSelected))
63
64 // populate dobDate with popDays
65 for(var i:Number = 1; i<=popDays; i++){
66
67 dobDate.addItem({data:i, label:i});
68
69 } // end for(var i:Number = 0; i<30; i++)
70
71 };
72
73 // apply listener to dobMonth and Year
74 dobMonth.addEventListener("change", cbListener);
75 dobYear.addEventListener("change", cbListener);
76
77 // create myYears, starting with this year
78 var myYears:Number = myDate.getFullYear();
79
80 // populate dobYear with deincrementing loop,
81 // starting with the current year and going back 80 years
82 for(var i:Number = myYears; i>(myYears-80); i--){
83
84 // populate dobYear data and label with i
85 dobYear.addItem({data:i, label:i});
86
87 } // end for(var i:Number=currentYear; i>(currentYear-80); i--)

What on Earth am I doing wrong? Is addItem an improper method for what I'm doing? If so, what would be better suited for this task?

Vagabond
03-12-2009, 01:18 AM
Any insight into this one?

Vagabond
03-12-2009, 01:41 AM
Turns out addItem alone was not the appropriate method to use, and I was just tacking on an infinite number of dates into the combo box.

what I needed was to clear the combo box, THEN repopulate it appropriately.

a-somethin like this:
// create a listener for dobMonth and dobYear
// when these are changed, repopulate dobDate
var cbListener:Object = new Object();
cbListener.change = function(evt_obj:Object):Void {

// set monthSelected and yearSelected
var monthSelected:Object = dobMonth.selectedItem;
var yearSelected:Object = dobYear.selectedItem;

// if "monthSelected" is in the array "monthArray30" (4,6,9,11)
if(monthArray30.in_array(monthSelected.data)){

// set popDays to 30
var popDays:Number = 30;

// else, if "monthSelected" is in the array "monthArray31" (1,3,5,7,8,10,12)
} else if(monthArray31.in_array(monthSelected.data)){

// set popDays to 31
var popDays:Number = 31;

// else, if "monthSelected" is 2 (february)
} else if(monthSelected.data = 2){

// determine if yearSelected is a leap year
// if "yearSelected" is divisible by 4, AND NOT 100, or divisible by 400
if(((yearSelected.data % 4 == 0) && (yearSelected.data % 100 != 0)) || (yearSelected.data % 400 == 0)){

// set popDays to 29 (leap year)
var popDays:Number = 29;

// else, if "yearSelected" is NOT a leap year
} else {

// set popDays to 28
var popDays:Number = 28;

} // end if((yearSelected % 4 == 0) && (yearSelected % 100 != 0)) || (yearSelected % 400 == 0)

} // end if(monthArray30.in_array(monthSelected))

// first, remove the current elements...
dobDate.removeAll();
dobDate.addItem({data:"default", label:"Date"});

// populate dobDate with popDays
for(var i:Number = 1; i<=popDays; i++){

// then repopulate
dobDate.addItem({data:i, label:i});

} // end for(var i:Number = 0; i<30; i++)

};

// apply listener to dobMonth and Year
dobMonth.addEventListener("change", cbListener);
dobYear.addEventListener("change", cbListener);

also, be sure to define the in_array prototype, or the whole thing will be pointless.

// USAGE:
// var hayStack = new Array("hello", "world", Array("one", "two"));
// trace(hayStack.in_array("hello")); // true
// trace(hayStack.in_array("hi")); // false
// trace(hayStack.in_array("two")); // true
Array.prototype.in_array = function(needle){

// if debug mode is on...
if(debug_in_array == true){

// execute traces
trace("in_array initiated...");
trace("Needle = " + needle + "...");
trace("Checking elements...");

} // end if(debug_in_array == true)

// starting at element 0,
// as long as there is another element,
// increment i and run the script
for(var i = 0; i < this.length; i++ ){

// if debug mode is on...
if(debug_in_array == true){

// execute traces
trace("Element " + i + " = " + this[i] + "...");

} // end if(debug_in_array == true)

// if the current element matches the needle...
if(this[i] == needle){

// return true for confirmed match
return true;

// if debug mode is on...
if(debug_in_array == true){

// execute traces
trace("Match found! Returning result...");

} // end if(debug_in_array == true)

// else, if the current element is an array...
} else if(this[i] instanceof Array){

// re-initiate in_array for that array
return this[i].in_array(needle);

// if debug mode is on...
if(debug_in_array == true){

// execute traces
trace("Multidimensional array detected! Recursing in_array...");

} // end if(debug_in_array == true)

} // end if(this[i] == needle)

} // end for(var i = 0; i < this.length; i++ )

// if nothing has been returned, there was no match
return false;

// if debug mode is on...
if(debug_in_array == true){

// execute traces
trace("All elements checked, no match found... Sorry");
trace("Thank you for using in_array.");

} // end if(debug_in_array == true)

} // end Array Prototype in_array

var debug_in_array:Boolean = false; // used to debug this prototype