This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to ake an input text field only accept numbers.  RSS feed

 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How can I enable <h:inputText> to be valid only for numbers.
Meaning when the user will push any button on the keyboard that it’s not a number then nothing will happen.

The inputText will suitable only for numbers

Thanks

 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although there is some movement towards smarter input controls these days, I think that the HTML INPUT TYPE=TEXT control is still basically stuck with unrestricted characters, and it's what determines what the JSF equivalent (inputText) tag will accept.

You can limit input the same way you would for basic HTML: attach a JavaScript keystroke filter to the control that rejects non-digit characters. Don't forget to allow for control characters, though, or people won't be able to tab out of the control!
 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you
 
Brendan Healey
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will do it:



Note that you can't use < or >, so you have to use & lt; and & gt; (without the space between the &), but this forum
is displaying it like it is. I think event.which is preferred to event.keyCode, but the latter is an option.

Regards,
Brendan.
 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
This is a nice solution
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!