0

I've been banging my head against the wall for days trying to get a KeyPress or a KeyDown event to fire on a Sencha Touch textfield.

I'm trying to accomplish two things: 1) Limit the max length to 3 and 2) Ensure that the user is entering only numbers (no letters or special chars)

Is there no way using Javascript to attach these events? I'm using Sencha 2.3.1

Update: I've tried using numberfield and also using maxLength...neither work.

   var numberfield = 
    {
        xtype: 'numberfield',
        value: textValue,
        readOnly: true,
        cls: 'opBuyoffPartialCell',
        inputCls: 'opBuyoffCenterText',
        maxLength: 3,
        enforceMaxLength: 3
    };
Sarah Lottman
  • 39
  • 2
  • 9
  • 1
    Why can't you just use a [numberfield](https://docs.sencha.com/touch/2.3.1/#!/api/Ext.field.Number) component instead of a textfield, with a maxLength of 3? – Monica Olejniczak Dec 23 '15 at 05:14

1 Answers1

0

As @MonicaOlejniczak wrote, use a numberfield.

If you don't want to use a numberfield, at least have a look at its code and find that Sencha uses

this.getComponent().input.dom.setAttribute("type", "number");

to ensure that the user can only input numbers. That way, the "number" ,,,, which your code would allow, will also be taken care of.

To ensure a maxLength, sencha already has maxLength config which is available on both the numberfield and the textfield.

Alexander
  • 19,906
  • 19
  • 75
  • 162
  • Sadly using a numberfield does not prevent the user from entering letters. Also, I've tried using maxLength - and it doesn't seem to have any effect on the number of allowed entries. I've pasted my updated code above. – Sarah Lottman Dec 23 '15 at 17:05