Hey all I am using Action Script and just simply trying to check if a string contains special characters or numbers, if it does not add it to my list otherwise error output…however the NAN is giving me the error Implicit coercion of a value of type String to an unrelated type Number…any suggestions? here is some code:
private function onAddName(event:Event):void {
var newItem:Object = {label:addName_ti.text};
//if not a number true then add onto list.
if ( isNaN(addName_ti.text )
{
names_li.dataProvider.addItem(newItem);
names_li.dataProvider.sortOn("label");
addName_ti.text = "";
addName_bn.enabled = false;
//User feedback for successfully entering item.
trace ("Item succesfully added!");
} else
{
//User feedback for not succesffully entering item.
addName_bn.enabled = false;
trace ("Improper formatting");
}
}
isNaN expects a number. That is why you get the error. You should use parseFloat first:
parseFloat is good especially for external text, because it will ignore white space and trailing characters. It will return NaN if the parse is unsuccessful.
" 12"would return12. “12abc” would return12. “abc12” would returnNaN." 12 34 5 6"would return12.If you’re text is guaranteed to be well formatted, the simply casting as a number
Number("12")would be slightly faster. In my test, doing it 10 million times was about 400ms quicker.