Be careful when parsing numeric entries from a text field or similar component in which the user is supposed to enter a numeric value as a string. If possible, set the restrict property on the input field to “0-9.” so as to only allow numerals and the decimal if non-integer input is expected. This is not sufficient however. In particular, be sure to use parseInt or parseFloat to convert the component’s String value into a number instead of the Number() function or the Number class constructor.

The reason for this is that a String literal beginning with a zero will be interpreted by Number as an octal value instead of a base-10 integer. Say we have a text input component priceInput into which the user has entered the value “010″, meaning “10″ with an extraneous leading zero (possibly because the field had been pre-populated with a value of zero). However, if we were to parse this input into a number using

var priceVal = Number(priceInput.text);

or

var priceVal:Number = new Number(priceInput.text);

we would end up with the unexpected value of 8 instead of 10, since the input string is interpreted as an octal value. Instead, use parseInt and specify a base of 10, or parseFloat if a non-integer value is expected:

var priceInt = parseInt(priceInput.text, 10);

or

var priceFloat = parseFloat(priceInput.text);

Note that these two parsing functions will return NaN if the input string is not parseable into a number, so it’s a good idea to use isNan() on the result to ensure that a legal numeric value was retrieved.