Restricting Text Input

I l @ ve RuBoard

When a user is entering data into a field, it is often a good idea to only allow him to type characters that make sense. For instance, if the user is asked what year he was born, he should only need the number keys to answer. There is never a need to type letters or other characters.

You restrict which characters can be typed into an input field by using the restrict property of that field. If you don't set this property at all, any character would be allowed in the field. However, if you set it to a string, only characters in that string are allowed.

Here is how you would set the input field text1 to only accept number characters:

 text1.restrict = "01234567890"; 

Suppose that you have a field where the user is supposed to type her e-mail address. E-mail addresses can only have the basic letters and numbers in them, plus the special characters "@" and ".". In addition, dashes and underscores are allowed in e-mail addresses. This is how you would restrict the user to only these characters:

 text2.restrict = "abcdefghijklmnopqrstuvwxyz0123456789@.-_"; 

The preceding line leaves one question: Will the input field accept both upper- and lowercase letters? Yes, it will accept both, so the restrict property ignores case.

You can also limit the number of characters allowed in an input field. This can be done without ActionScript because there is a Maximum Characters option in the Properties panel. If you set this to 32, only 32 characters would be allowed in the field. You can also set this property with ActionScript using the maxChars property. For instance, if you wanted to limit a field to only accept years , you would want it to only accept digits, and only four of them:

 text1.restrict = "01234567890"; text1.maxChars = 4; 

The example movie 15restrict.fla contains the preceding code and sample input field for you to try out.

I l @ ve RuBoard

Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272 © 2008-2017.
If you may any questions please contact us: