Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function to test if a String variable is a number value?

Is there a way to test a string, such as the one below to see if it's an actual number value?

var theStr:String = '05';

I want to differentiate between the string value above and one such as this:

var theStr2:String = 'asdfl';

Thanks!

like image 786
Mike Moore Avatar asked Jun 16 '10 19:06

Mike Moore


People also ask

Can a string value have numbers?

A string consists of one or more characters, which can include letters, numbers, and other types of characters. You can think of a string as plain text. A string represents alphanumeric data.


2 Answers

Yes use isNaN function to test if it the String is a valid Number:

var n:Number=Number(theStr);
if (isNaN(n)){
 trace("not a number");
} else {
 trace("number="+n);
}
like image 179
Patrick Avatar answered Nov 15 '22 08:11

Patrick


You must cast to Number to get is NaN. If you use int letters can be cast to 0.

like image 34
FlashJonas Avatar answered Nov 15 '22 08:11

FlashJonas