Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to ake an input text field only accept numbers.

 
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
Saloon Keeper
Pie
Posts: 18214
53
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