Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

RichFaces 4 h:inputText allow only numbers?  RSS feed

 
Edson Cezar
Ranch Hand
Posts: 119
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a field in JSF2, I tried execute a validation in this way:




And thus:



However, my field only makes a validation and show me a message, I would like just the insertion of numbers, not Strings, this is possible ?



Is possible a insertion only of numbers ?

Thanks in advance for all !
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ironically, the old IBM mainframe "dumb green screen" terminals had that ability, but not HTML GUIs.

I believe that HTML5 supports not only limiting input text, but also (on mobile devices), the intelligent selection of virtual keyboards (numeric keypad pop-up instead of a full text keyboard).

Unfortunately, RichFaces 4 is still stuck on HTML4, I believe, so that feature isn't available. The best you could do is attach a JavaScript to the keydown or keypress event of that control. Do a web search and you should be able to find examples. Look for "HTML numeric only input", since it's not specifically a RichFaces thing.
 
Edson Cezar
Ranch Hand
Posts: 119
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this, it´s work well:



But this not allow a click on the backspace button, in case the user make a mistake.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the idea.

What I recommend, however, over doing it as inline javascript is that you code that as a javascript function call. That way you can A) code longer, more complex logic (including backspace/Enter handling) B) recycle the code in the event you have other numeric-only controls and C) export the function text to an external ".js" file. Which is easier to use a browser's javascript debugger on.
 
Edson Cezar
Ranch Hand
Posts: 119
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finished using this function:



This work well in IE and Chrome, I don´t know why it´s not work well in firefox too, this function block the tab key in Firefox.

I found this post too:

HTML Text Input allow only Numeric input

Thanks Tim, Thanks for all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!