Hello everyone,
I have a simple question. I am running an if statement to determing whether the data entered by a user is a number or a combination of letters and/or numbers. How could I determine whether the data is just numbers? Like:
if (textbox == notanumber) {
do something
}
I have tried using NaN and “undefined” without much success.
Do you have to determine it with an IF statement? if so this is probably useless!
I think all characters (at least most) relate to a keycode (possibly ASCII) number for example
A=65, B=66
numbers do also
0=48, 1=49 etc.
you could put the enterred data into an Array, 1 character per array element then test each element.
If each element’s key code is between 48 and 57 then the data eneterred is all numbers. Once you have determined what the enterred data consists of you could then use your if statement, to do whatever.
I have never used NaN statements, but I would guess for an IF statement that you would use NaN in someway.
myLetters="abcdefghijklmnopqrstuvwxyz";
myText="1re3";
myBool=false;
for (i=0;i < myLetters.length;i++){
value=myText.indexOf(myLetters.charAt(i));
if (value!=-1){
myBool=true;
break;
}
}
if (myBool) trace ("There are letters in that text");
else trace ("No letters");
myLetters is a string containing all the letters of the alphabet.
myText is the text you’re testing.
myBool is false to start with, and remains false if there are no letters.
We browse through the letters of the alphabet to check if one of them is in the string. myLetters.charAt(i) returns each letter from the alphabet independently, and myText.indexOf checks if that letter is in myText.
If it is not, it returns -1, otherwise it returns the index of the letter. So if the index is not -1, FLash has found the letter, hence there is a letter in the string.
Hey iammontoya,
That was it! That line of code solved my problem. Thanks a bunch RageW0lf and Iammontya. Thanks to you as well Pom, I am sure that must have taken some effort to do all that.
no problem, man! That’s why we’re here, to help each other out! Hey, Kirupa… I have a new tutorial ready for you, if you’d like it. Where to send again?