Determining if data is a number or word combination

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.

Thanks,
Kirupa :rambo:

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.

You could try to find a letter in your text, starting from a to z, and if you find one, then, well, do whatever you want to do.

I believe you’ll need the indexof function. I’ll try and come up with something.

pom :asian:

There you go:

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.

Tadaaa!

pom :asian:

Hey guys!

I think you’re looking for this:

if(isNaN(textbox)){

if textbox is not a number, it will return a value of True, therefore executing your code in the IF statement.

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.

Cheers!
Kirupa :bandit:

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?

:stuck_out_tongue: Don’t worry Kirupa, it was fun! And Inigo, nice trick!

pom :asian: