RKCY
RKCY

Reputation: 4135

Restrict numbers 1-10 in textInput Box in Flex

I have a TextInput box. I have to restrict a range more than 1,2,3,4,5,6,7,8,9,10 numbers only in the text Box.If i enter any number above that it should be allowed. If enter 11 or more than that, it should not allow to enter. it should allow from 1- 10 digits in the text box. If enter 11 or 0 it should not allow and more than 10 should not allowed.

Please help me how to restrict it in TextInput Box in Flex. If any one knows about thte regular expression, please help me out .

Upvotes: 1

Views: 6375

Answers (3)

JeffryHouser
JeffryHouser

Reputation: 39408

You can restrict the characters entered into a TextInput using the restrict property. If you only want to allow numbers entered, you can do this:

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

You can restrict the total number of characters entered into the TextInput using the maxChars property.

<s:TextInput maxChars="10" />

There is no reason they can't be combined

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

If you're only accepting numeric input, you could also use a NumericStepper component.

<s:NumericStepper maximum="1" maximum="9999999999" />

I'm not sure if there is an upper limit on NumericSteppers.

Does that answer your question?

Upvotes: 3

Dennis Jaamann
Dennis Jaamann

Reputation: 3565

If the only thing that the user can input are numbers 1 to 10,
try using the NumericStepper component instead.

This component has the features you describe built in.

For example:

<s:NumericStepper minimum="1" maximum="10" value="0" stepSize="1" maxChars="10"/>

Where:

  • minimum is the minimum allowed value
  • maximum is the maximum allowed value
  • value is the initial value
  • maxChars is the amount of characters allowed
  • stepsize is the amount by which the amount can be increased or decreased with the arrow buttons

Check out the livedocs:

Cheers

Upvotes: 5

Santhosh Nayak
Santhosh Nayak

Reputation: 2288

Use mx:TextInput because it has textInput event

<mx:TextInput id="ti" restrict="0-9" textInput="Myfun(event)"/>

then your Myfun will be:

private function Myfun(ev:TextEvent):void
        {
            if(Number(ti.text + ev.text)>10)
            ev.preventDefault();
        }

This will not allow you to type numbers greater than 10

if you want 10 digits use..

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

Upvotes: 0

Related Questions