I have an input field where the user can enter a zip code, or a city/state combo (not to be confused with up, up, down, down, left, right, left, rigth, B, A, select, start) to search some things…
The action on my “go” button is as follows, where searchItem is the variable name of the input box:
[AS]on(release){
trace(“THE SEARCH ITEM IS: “+searchItem);
if(typeof searchItem == “number”){
trace(“THIS IS A ZIP CODE SEARCH”);
if(searchItem.length > 5){
trace(“ZIP CODE IS TOO LONG, ANGRY TIGER!”);
}
} else {
searchItem.split(”,”);
searchCity = searchItem[0];
searchState = searchItem[1];
trace(“THIS IS A CITY/STATE SEARCH: “+searchCity+”,”+searchState);
}
}[/AS]
But alas… all the validation fails. It displays searchItem, but not the results of the split, nor does it recognize the variable as a number type when a zip code is entered…
You want to compare a String to a Number right? So check the string value compared to the number value of the inputted text… [AS]on (release) {
trace(“THE SEARCH ITEM IS: “+searchItem);
if (String(searchItem) == Number(searchItem)) {
trace(“THIS IS A ZIP CODE SEARCH”);
if (searchItem.length>5) {
trace(“ZIP CODE IS TOO LONG, ANGRY TIGER!”);
}
} else {
searchItem.split(”,”);
searchCity = searchItem[0];
searchState = searchItem[1];
trace(“THIS IS A CITY/STATE SEARCH: “+searchCity+”,”+searchState);
}
}[/AS] I THINK that should work.
Ok, I tested the above code and it works, for the most part, but I see a few more errors. You are splitting wrong. You are supposed to assign that as a variable, something like [AS]searched = searchItem.split(",");[/AS] and then from there the searched variable will be your array, so it would be [AS]on (release) {
trace(“THE SEARCH ITEM IS: “+searchItem);
if (String(searchItem) == Number(searchItem)) {
trace(“THIS IS A ZIP CODE SEARCH”);
if (searchItem.length>5) {
trace(“ZIP CODE IS TOO LONG, ANGRY TIGER!”);
}
} else {
searched = searchItem.split(”,”);
searchCity = searched[0];
searchState = searched[1];
trace(“THIS IS A CITY/STATE SEARCH: “+searchCity+”,”+searchState);
}
}[/AS] But aside from that, I can’t seem to figure out why you would split it at the “,” only to put it back together the same way it was originally written. Why don’t you just do… [AS]on (release) {
trace("THE SEARCH ITEM IS: "+searchItem);
if (String(searchItem) == Number(searchItem)) {
trace(“THIS IS A ZIP CODE SEARCH”);
if (searchItem.length>5) {
trace(“ZIP CODE IS TOO LONG, ANGRY TIGER!”);
}
} else {
trace("THIS IS A CITY/STATE SEARCH: "+searchItem);
}
}[/AS] It returns the same results in less code.
What I’m trying to do here is take one input, check to see if it’s a number or a string, and then go from there, you know?
The string get’s split because I’m going to need the city and state apart for various evil deeds, muhuhahahahahaha ahem… I only put them back together in the trace function to make sure they were coming out alright
But the split works ok… what I’m stuck on is how to say “check this variable,w hich is a sting, and if it’s a number, do this, if not, go to town, alphanumeric style”…
Here’s what I tried:
[AS]if(isNaN(Number(searchItem)) == “false”)[/AS]
but that doesn’t seem to be working out for me :hat:
Hahah… Master Lostinbeta, forgive me for my insolence
I didn’t know you could just do ! at the beginning, or that false shouldn’t be in quotes… where did that wily ActionScript book go off to… I should really go through it again!
Once again you have helped my brain untangle itself while caught in the pilons of the old wooden dock along the shore of the rocky beach we all know and love as “ActionScript”!
! is called a logical operator, and it basically checks if the expression following it is false, and if that statement is false, it ironically returns true (since it checks if its false and it is false) and thus the code within the if statement is activated I often like to this of ! as saying “not”. Usually makes sense. Something like… [AS]tired = false;
if (!tired){
me.gotoAndSleep(“bed”);
}[/AS] See how that would be like saying if “not tired”. I dunno, I just always found that helped me with it, maybe it will help you too.
And false shouldn’t be in quotes because quotes typically define a string, so you would be checking if the string value equaled “false”, but really you want to check the boolean value.
Confusing eh…lol. Flash does have it’s little quirks.