Yordan Yanakiev
Yordan Yanakiev

Reputation: 2604

Remove character inside TextInput while typing in it ? ( Flex 4.5 )

I wish to remove specific characters inside Spark TextInput while user typing on it, without this causing any distrbance like licking with mouse after the last character or alike.

Upvotes: 2

Views: 1852

Answers (2)

bmleite
bmleite

Reputation: 26870

You can create your own custom TextInput component and override the keyDownHandler() or you can add a event listener on the TextInput, like this:

<s:TextInput keyDown="{ textInputKeyDownHandler(event) }"/>

and then on the event handler:

private function textInputKeyDownHandler(event:KeyboardEvent):void {
    // Make your validations and if necessary, use the following command 
    // to prevent the character from being added to the TextInput
    event.preventDefault();
}

This way the character will never be added to the TextInput, which means the text property and the cursor position will not change.

Note: Use the event.charCode and event.keyCode to make the necessary validations.

Upvotes: 1

Harry Ninh
Harry Ninh

Reputation: 16718

Have you tried restrict property of the TextInput? I don't know what are your specific characters, but commonly there are 2 cases of restriction. Restrict to a set of characters:

<s:TextInput restrict="A-Za-z" />

Allow all characters except some special characters:

<s:TextInput restrict="^0-9" />

To deal with unicode characters, use \u:

<s:TextInput restrict="\u0239" />

Upvotes: 1

Related Questions