views:

954

answers:

1

Hi,

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

A: 

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+\-]"
}
Amarghosh
you don't need brackets ([ and ]) in restrict field as TextInput.restrit is String type. And restrict only can force individual characters to be enabled or disabled. For achieving your requirements you need to check the text String against your regular expression and if it is not valid then remove the last entered character. So use restrict for restricting user inputs for only those characters (0-9+-) and use regex match to verify whenever TextInput changes.
bhups
I tested this code and it's working perfectly. It sounds sensible to omit square brackets, but strangely my text input didn't accept `-` without square brackets (with or without escaping).
Amarghosh
hi man , its allowing alphabets in first character and some symbols ..Its not possible to do in single Regexpression ?,Is this the only way doing on the change event!!...Thanks for ur reply man.
vineth
No, it's not allowing alphabets or symbols - I tested again. What version of flex are you using? You cannot do it with a single regex - the restrict property takes a string and not a regex. http://livedocs.adobe.com/flex/3/langref/mx/controls/TextInput.html#restrict
Amarghosh