Results 1 to 4 of 4

Thread: Is there a way to get numberfield to accept decimal numbers without the leading 0?

  1. #1
    Sencha User
    Join Date
    Apr 2012
    Posts
    37
    Answers
    1
    Vote Rating
    5
      0  

    Default Answered: Is there a way to get numberfield to accept decimal numbers without the leading 0?

    If you type 0.7 in a numberfield it will accept the value, but if you type .7 it will clear it out. Is there a way to get this field to accept the shortened version? Obviously if the user types .7 he means 0.7.


    Example code of it occurring using Sencha Touch 2.1:

    Code:
    Ext.Viewport.add(
    {
       xtype: 'formpanel',
       items:
          [
          {
              id: 'TestField',
             xtype: 'numberfield',
             label: 'Type .7'
          },
          {
              xtype: 'button',
              text: 'Now Click Here',
              handler: function ()
             {
                 Ext.Msg.alert('', 'You entered: ' + Ext.getCmp('TestField').getValue() + '.');
             }
          }
       ]
    });

  2. Using this test:

    Code:
    Ext.Viewport.add({
        items : [
            {
                xtype     : 'numberfield',
                label     : 'Test',
                listeners : {
                    change : function () {
                        console.log('change');
                    }
                }
            }
        ]
    });
    In Chrome on the desktop I get the change and the value is .7. Testing on an ipad, the change event fires but indeed the value is set to 0.7 which is browser behavior as we aren't doing any parsing when you type. If you were to execute setValue then we do a parseFloat or when you execute getValue then we also do a parseFloat.

  3. #2
    Sencha User
    Join Date
    Apr 2012
    Posts
    37
    Answers
    1
    Vote Rating
    5
      0  

    Default

    Anyone?

    I tried using the change event to listen for it so I can add the 0.7 on the fly, but the event doesn't even fire if you type in ".7". The field just reverts to whatever was in it previously.

  4. #3
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,553
    Answers
    3931
    Vote Rating
    1272
      0  

    Default

    Using this test:

    Code:
    Ext.Viewport.add({
        items : [
            {
                xtype     : 'numberfield',
                label     : 'Test',
                listeners : {
                    change : function () {
                        console.log('change');
                    }
                }
            }
        ]
    });
    In Chrome on the desktop I get the change and the value is .7. Testing on an ipad, the change event fires but indeed the value is set to 0.7 which is browser behavior as we aren't doing any parsing when you type. If you were to execute setValue then we do a parseFloat or when you execute getValue then we also do a parseFloat.
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  5. #4
    Sencha User
    Join Date
    Apr 2012
    Posts
    37
    Answers
    1
    Vote Rating
    5
      0  

    Default

    It definitely does seem to be a browser specific issue. I was testing on desktop Safari, which I probably should have mentioned, and it doesn't fire the change event and resets the field instantly. Thanks for the reply and helping me understand what was going on.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •