AS3 Quickie: Check if a variable is Numeric

I’m still doing loads of ActionScript work in both my regular job and in side projects – so thought I might start posting some tiny, useful snippets of AS3 here as I encounter them. This is, I suppose, both for my own archive and for anyone out in the community that cares to pick up on these bits and pieces.

So this quickie comes from a project where I actually have an AIR application running as a service on a Windows 2008 virtual machine. The application monitors a remote folder for uploads from a web application in order to process these uploaded files for distribution.

The files themselves are renamed, upon upload, to a numeric value representing the ID of the object they are associated with in the web application. The server-based AIR app uses this ID to communicate with both the web application and an additional, remote Flash Media Server instance.

The problem is that, sometimes, when the file is uploaded… previous to it being renamed by ColdFusion… it will get snatched up by this AIR app and so the file name is whatever the user named the file previous to upload – not the expected system designated ID. So what’s the solution? Check to see whether the file name is truly numeric or not. Granted – if the user creates a file for upload which is numeric, it will get around this code… the chance of that happening is quite slim though, and even if it does happen, can be dealt with in other ways down the road.

var fileName:String = "2739.mp4";
var extIndex:int = fileName.indexOf(".");
var fileID:Number = Number(fileName.substr(0, extIndex));
if(!isNaN(fileID)){
	trace("Ok! Process file.");
}else{
	trace("Wait for next cycle...");
}

Leave a Comment

Your email address will not be published. Required fields are marked *