Archive for 'Actionscript'

Using a hyphen in a post variable using LoadVars

If you’ve ever tried to use LoadVars to send a form that requires a hyphen in the variable name, you might be pulling your hair out right now.  For example, lets say you’re trying to submit a form variable like this:

loginin-id=15

So, in Flash, you probably tried something like this:

var loadVarsSender:LoadVars = new LoadVars();
loadVarsSender.login-id="Some data";
var loadVarsReceiver:LoadVars = new LoadVars();
loadVarsSender.sendAndLoad("somepage.php", loadVarsReceiver, "POST");

But the problem is that Flash sees that as login minus id.  It’s treating the hyphen as a subtraction operator.  The solution, wrap the variable name in square braces like so:

var loadVarsSender:LoadVars = new LoadVars();
loadVarsSender["login-id"]="Some data";
var loadVarsReceiver:LoadVars = new LoadVars();
loadVarsSender.sendAndLoad("somepage.php", loadVarsReceiver, "POST");

That’ll tell flash that the hyphen is part of the variable name.  This should work for any other strange characters that you need to use in the variable name too.

var loadVarsSender:LoadVars = new LoadVars();
loadVarsSender.text_org=”Some data”;
var loadVarsReceiver:LoadVars = new LoadVars();

loadVarsSender.sendAndLoad(“somepage.php”, loadVarsReceiver, “POST”);

Only allow numbers in an input field in AS2

If you’ve ever created a form using ActionScript 2, you know that sometimes you want to limit what characters a user can type into a given field. For example, if you’re asking the user to enter their age, or a zip code, they certainly don’t need to use letters. So you can restrict the user to only be able to type certain keys on the keyboard when they’re in that field. Here’s how:

myinputfield_txt.restrict = “0-9″;

This tells Flash to only allow the numbers 0-9 to be typed into that field.