15

Not sure if this makes sense, but I need to check if a server value returned is actually a number. Right now I get ALL number values returned as strings ie '7' instead of 7.

What's the simplest way to check if string values can actually be converted to numbers?

Bachalo
  • 6,965
  • 27
  • 95
  • 189

11 Answers11

24

The easiest way to do this is to actually convert the string to a Number and test to see if it's NaN. If you look at the Flex API reference, the top-level Number() function says it will return NaN if the string passed to the method cannot be converted to a Number.

Fortunately, Flex (sort of) does this for you, with the isNaN() function. All you need to do is:

var testFlag:Boolean = isNaN( someStringThatMightBeANumber );

If testFlag is false, the string can be converted to a number, otherwise it can't be converted.

Edit

The above will not work if compiling in strict mode. Instead, you will need to first convert to Number and then check for NaN, as follows:

var testFlag:Boolean = isNaN( Number( someStringThatMightBeANumber ) );
Dan
  • 836
  • 1
  • 5
  • 11
16

Haven't tested this, but this should work:

if( isNaN(theString) ) {
   trace("it is a string");
} else {
    trace("it is a number");
}

If you are using AS3 and/or strict mode (as pointed out by back2dos), you will need to convert to number first in order for it to compile:

if( isNaN(Number(theString)) ) {
   trace("it is a string");
} else {
    trace("it is a number");
}
OneNerd
  • 6,442
  • 17
  • 60
  • 78
  • the parameter of isNaN has to be a float ... so this will throw compiler errors in strict mode ... – back2dos Aug 06 '09 at 18:08
  • 5
    true for as3 strict mode I suppose, although that was not specified. Hardly deserves a negative vote though dude - I mean, you voted down 3 posts here. How about just putting in a comment and leaving it at that. jeesh. – OneNerd Aug 06 '09 at 18:17
4

Most of the answers on this question have a major flaw in them. If you take Number(null) or Number(undefined) or Number(""), all will return 0 and will evaluate to "is a number". Try something like this instead:

function isANumber( val:* ):Boolean {
    return !(val === null || val === "" || isNaN(val));
}
Justin
  • 63
  • 3
3

RegExp path :

function stringIsAValidNumber(s: String) : Boolean {
    return Boolean(s.match(/^[0-9]+.?[0-9]+$/));
}
OXMO456
  • 3,558
  • 2
  • 25
  • 35
1

Here is another way to check if value can be converted to a number:

var ob:Object = {a:'2',b:3,c:'string'};

for( var v:* in ob){
 var nr:Number = ob[v];
 trace(ob[v]+" "+(nr === Number(nr)))
}

this will trace following:

2 true
3 true
string false
Greg Mattes
  • 33,090
  • 15
  • 73
  • 105
dki
  • 11
  • 1
0

You can notice that in actionscript :

trace(int('7')); // will return 7

and

trace(int('a')); // will return 0

So except for zeros, you can actually now if a string is a number or not

afewcc
  • 1,077
  • 3
  • 9
  • 20
0

this will try to convert your String to a Number, which essentially is a 64 bit floating point number:

var val:Number = Number(sourceString);

if sourceString is not a valid String representation of a Number, val will be NaN (not a number) ... you have check against that value with isNaN ... because val == NaN will return false for a reason that can't quite understand ... you can use int(val) == val to check, whether it is an integral value ...

greetz

back2dos

back2dos
  • 15,588
  • 34
  • 50
0
function isANumber(__str:String):Boolean
{
    return !isNaN(Number(__str));
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
0

You should use the native solution of Adobe:

parseInt and parseFloat methods.

Also read the isNaN description:

Returns true if the value is NaN(not a number). The isNaN() function is useful for checking whether a mathematical expression evaluates successfully to a number. The most common use of isNaN() is to check the value returned from the parseInt() and parseFloat() functions. The NaN value is a special member of the Number data type that represents a value that is "not a number."

Here is a simple implementation:

function isANumber(value:String):Boolean {
    return !isNaN(parseFloat(value));
}
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
  • This method will also return true in case string contains number with random characters, example: 6s. – krizajb Jul 18 '16 at 22:10
0

Put this into any function where you want only numbers to stay
joy_edit1 is a TextInput Object (spark)

//is a number check
if( isNaN(Number(joy_edit1.text)) ) {
  joy_edit1.text = "";
  return void;
}
GMD
  • 698
  • 5
  • 12
Rama
  • 41
  • 1
-1
typeof('7') == 'string'
typeof(7) == 'number'

Does that help?

airportyh
  • 21,948
  • 13
  • 58
  • 72
  • 2
    not really, since it was about determining whether a string is numeric ... btw. this is deprecated since AS3, since it provides "is" operator (AS2 had "instanceof" for that matter), property "constructor" or "flash.utils.getQualifiedClassName" – back2dos Aug 06 '09 at 18:12