Author
Allow only numbers?
Nebojsa Karan
Greenhorn
Joined: Oct 28, 2009
Posts: 2
I have one question.
How to allow only numbers to enter in text box and forbid letters and other non-numeric characters?
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12617
posted Oct 28, 2009 08:16:55
0
What kind of text box? Swing? If so, I'll move it to the appropriate forum. HTML? If so, I'll move it to another different appropriate forum. If HTML, are you using a framework? If so, I'll move it to possibly yet another different appropriate forum. Using JavaScript?
Nebojsa Karan
Greenhorn
Joined: Oct 28, 2009
Posts: 2
Swing
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
Welcome to JavaRanch, Nebojsa
As far as I know there is no single method to do that.
You may do it by handling the user input - just have a look at this example .
Author of ExamLab (Download ) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
Rob Camick
Ranch Hand
Joined: Jun 13, 2009
Posts: 1788
The above example is an old example.
Use either a JFormattedTextField or add a DocumentFilter to the Document.
More information about both options can be found in the Swing tutorial.
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
Something I never used. Great
subject: Allow only numbers?