Text Input restrict in Flex3 Air

actionscript-3airapache-flex

I need to restrict the user and allow only first character as + or – or 0-9 and other character as 0-9..how can i do this

in regular expression validator the below expression works but i need in restrict field.

<mx:TextInput id="txtTop"  restrict="[0-9+-][0-9]*$" />

Valid values are

+023

-123

23

0

invalid

+-123

fsaf

-+2132

Thanks in advance

Best Answer

Change the value of restrict based on the length of the string.

<mx:TextInput id="ti" restrict="[0-9+\-]" change="onChange(event)"/>

private function onChange(event:Event):void
{
    if(ti.text.length > 0)
        ti.restrict = "[0-9]";
    else
        ti.restrict = "[0-9+\-]"
}