aspose file tools*
The moose likes JSF and the fly likes How to ake an input text field only accept numbers. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to ake an input text field only accept numbers." Watch "How to ake an input text field only accept numbers." New topic
Author

How to ake an input text field only accept numbers.

chen young
Ranch Hand

Joined: Sep 09, 2005
Posts: 190
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

Joined: Jun 25, 2001
Posts: 16065
    
  21

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!


Customer surveys are for companies who didn't pay proper attention to begin with.
chen young
Ranch Hand

Joined: Sep 09, 2005
Posts: 190
Thanks you
Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218
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

Joined: Sep 09, 2005
Posts: 190
Thanks,
This is a nice solution
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to ake an input text field only accept numbers.