Friday, March 7, 2008

How to restrict the Backslash(\) character

In ActionScript, if you want to restrict the characters that can be entered into a TextField (or TextInput), you use the restrict property. Check here for documentation.

For example, input_txt.restrict = "A-Z";
This restricts the characters to upperspace characters and disallows everything else.

And input_txt.restrict = "^0-9";
The ^ symbol indicates that whatever comes after that is disallowed. Here, the numbers 0 to 9 are not allowed, everything else is allowed.

The interesting part is what if you want to restrict the Backslash(\) character?

Apparently, the right way to do it is:
input_txt.restrict = "^\\\\";


Explanation?

From the livedocs documentation:
"You can use a backslash to enter a ^ or - verbatim. The accepted backslash sequences are \-, \^ or \\. The backslash must be an actual character in the string, so when specified in ActionScript, a double backslash must be used. "

So, the first two backslashes are as required, when you add the third backslash, the flex compiler thinks that you are trying to escape the " character. Hence you need the fourth backslash in the string.

Very interesting!

Labels: ,


Wednesday, April 25, 2007

Flash, Flex and ActionScript

Hello world!

I am a long time Flash/ActionScript developer. I currently work as a software developer in the Bay Area.

Having recently started working full time with Flex 2 and ActionScript 3, I have been facing a number of issues while trying to get things done. Many a time, a blog post by some Flex developer somewhere in the world explaining how he/she solved a particular problem has helped me a lot.

The same way, I am going to start posting about how I solved certain issues/challenges while working with Flex. Also, I will be posting about unsolved issues that I cant figure out solutions for; hopefully someone would respond :-)

Labels: , ,


This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]