Skip to main content
August 8, 2011
Answered

Prevent Specific Characters From Entering Textfield.

  • August 8, 2011
  • 1 reply
  • 554 views

Hello,

There is an attribute .restrict but as the defined it only prevents other characters from entering the textfield, restricting the textfield to the given characters.

I want to prevent characters 1-10 from entering the textfield.

The code below does not work the right way.

txt.border = true; //


function down(event:KeyboardEvent):void

{

     /*

     for (var i:uint=48; i<58; i++)

     {

          if (event.keyCode == i)

          {

               txt.text = txt.text.replace(/.$/,"");

          }

     }

     */

     //No ASCII, No RegExp

     for (var i:uint=0; i<10; i++)

     {

          if (txt.text.indexOf(String(i)))

          {

               txt.text = txt.text.replace(String(i),"");

          }

     }

}


stage.addEventListener(KeyboardEvent.KEY_DOWN, down);

There must be surely be a built in method.

This topic has been closed for replies.
Correct answer Ned Murphy

You can restrict the entry of specific characters using the ^ character... only one problem with your intentions... 10 is not a character...

tfield.restrict = "^0-9";

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
August 8, 2011

You can restrict the entry of specific characters using the ^ character... only one problem with your intentions... 10 is not a character...

tfield.restrict = "^0-9";

August 8, 2011

I meant 0 - 9.

RegExp-like again. At least it works, I guess this is the only way.

Ned Murphy
Legend
August 8, 2011

There are numerous ways to do what you want.  I was just showing how you could do it with what you said you couldn't use.