Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
lcaro
Posts: 95
Registered: ‎10-19-2010
My Device: Blackberry 8110

Re: Preventing non numeric key presses in a TextField

Ok, thanks for quick reply.

 

Hopefully RIM will address it. Fingers crossed :smileyhappy:

 

Regards

Developer
theappspod
Posts: 122
Registered: ‎11-12-2010
My Device: Dev Alpha

Re: Preventing non numeric key presses in a TextField

Can anyone help with this pleasetethering only way I could see this working was to remove all non numeric characters in ontextchanging but this didn't work. Is there anyway that I can detect what key has been pressed and cancel it if needed? I have seen one other app do this but have no idea how.

 

Many thanks.

Developer
theappspod
Posts: 122
Registered: ‎11-12-2010
My Device: Dev Alpha

Re: Preventing non numeric key presses in a TextField

I have been searching and searching for a solution to this and have found a possability.

 

https://developer.blackberry.com/cascades/reference/qevent.html

 

I have not come accross QEvents but it does have a Keypress event.  Anyone used this before?

Developer
theappspod
Posts: 122
Registered: ‎11-12-2010
My Device: Dev Alpha

Re: Preventing non numeric key presses in a TextField

Have managed to get numeric key presses working.

 

I created a javascript function as follows:

 

            // Strips out non numeric characters allowing only 0-9 and '.'
            function numericOnly (textin) {
             
                 var m_strOut = new String (textin);
                 m_strOut = m_strOut.replace(/[^\d.]/g,'');
                 
                 return m_strOut;
                                     
            } // end numericOnly

 

and then in the onTextChanging event did this:

 

onTextChanging: {
    txtAmount.text = rootContainer.numericOnly(text);
}

 

 Hope this helps someone.

 

 

 

Developer
georg22
Posts: 247
Registered: ‎11-22-2011
My Device: Blackberry Playbook

Re: Preventing non numeric key presses in a TextField

Thanks!

Developer
vencedor
Posts: 18
Registered: ‎11-08-2012
My Device: Blackberry 10 Dev Alpha B

Re: Preventing non numeric key presses in a TextField

there are more options with TextArea or TextField property inputMode, unfortunately I didn't see anything that limits to numeric only. the closest I found is TextFieldInputMode.Pin

        inputMode : TextFieldInputMode.Pin

If you find my post helpful, please click like :smileyhappy:
Developer
georg22
Posts: 247
Registered: ‎11-22-2011
My Device: Blackberry Playbook

Re: Preventing non numeric key presses in a TextField

@vencedor: TextFieldInputMode determines, which kind of keyboard apperas. In this case i use "TextFieldInputMode.NumbersAndPunctuation", but unfortunally it still has has keys, i dont want the user to use.

Developer
Titoeuf
Posts: 15
Registered: ‎07-12-2012
My Device: BlackBerry Z10 Limited Edition

Re: Preventing non numeric key presses in a TextField

Hi theappspod,

 

You're solution is very helpful but, for a weird reason, it seems possible to "break" the function.

 

The function works great for normal input situation, but i found out that if you spam the textfield with non-numeric characters the function could "break",  allowing the user to enter non-numeric characters again and you have to restart the application for the function to work again.

 

Anyone has the same behavior or have a fix for this? On my side, I'm trying to look further into this and I'll post a solution if I ever find one.

 

I really hope RIM makes something for this because numeric only input field could be of use in a lot of applications.

Developer
joelajean
Posts: 132
Registered: ‎01-25-2010
My Device: Z30, Z10, Q10, Dev Alpha B & Bold 9900

Re: Preventing non numeric key presses in a TextField

For a currency field... here is was I use... that's until RIM come up with a Currency solution :smileyhappy:

 

  // Strips out non numeric characters allowing only 0-9 and '.'
  function currencyOnly(text) 
  {
    // First clean and only keep numerics and .
    var result = new String(text);
    result = result.replace(/[^\d.]/g,'');  
    
    // Check if more then on . is inputted    
    if (text.split(".").length >= 3)
    {
      console.debug("Field must only have one .");        
      result = text.substring(0, text.length - 1);
    }      
    
    // Next we verify that only two decial are keeped.        
    var decimal = text.indexOf(".");
          
    if (decimal != -1)
    {
      if (decimal + 3 < text.length)
      {
        console.debug("Field must have only 2 decimal places");
        result = text.substring(0, decimal + 3);
      }        
    }
   

    return result;                      
  }

 

 

Author of Shopping Cart Calculator, BP Monitor & Book-zy
@kodybb on Twitter

Developer
Developer
klz
Posts: 132
Registered: ‎08-24-2011
My Device: Z10 LE + Playbook

Re: Preventing non numeric key presses in a TextField

great! but how to allow users to input negative values too?

Thank you!
----------------------------
My Apps: Alchemy Game | Image Resizer | Jam! | Traffic Signs: USA | Radio Record
My Blog: http://bb10pro.com