Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Allow only numbers?

 
Nebojsa Karan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4402
251
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Camick
Ranch Hand
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
ExamLab Creator
Marshal
Pie
Posts: 4402
251
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Use either a JFormattedTextField or add a DocumentFilter to the Document.

Something I never used. Great
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic