VoodooBurger
VoodooBurger

Reputation: 233

As3 input-field number condition

I'm trying to validate a flash form. In one case I need to know how to check if the value is a number.

If someone could show me this if-sentence, i would be very grateful! :)

Upvotes: 0

Views: 1317

Answers (1)

Patrick
Patrick

Reputation: 15717

Use Number to cast a String to a Number and then isNaN to check if the result is a Number:

var str:String="1234";

var n:Number = Number(str);
if (isNaN(n)) {
 trace(str+" is not a number");
} else {
 trace(str+" is a number");
}

str="12a4";
n=Number(str);

if (isNaN(n)) {
 trace(str+" is not a number");
} else {
 trace(str+" is a number");
}

Upvotes: 1

Related Questions