Solved

Validating a TextField in an Applet

Posted on 2002-03-21
4
290 Views
Last Modified: 2010-05-18
Hi everyone!
This is an easy question:
How can I validate a TextField to be sure that it contains only numbers? If it has something else I want to Open a Message Box informing the problem and return the focus to it.
This must be done in an Applet.

Thanks for any help in Advance!

Regards,
MRXZ
0
Comment
Question by:mrxz
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Add a KeyListener to the TextField and disallow anything that's not part of a number
0
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
> Add a KeyListener to the TextField and disallow anything that's not part of a number

they'll still be able to paste text into the textbox... to be sure, you can check the contents when they submit (click the button or whatever)

try {
   int textVal;
   textVal = Integer.parseInt("<text in textbox>");
} catch (NumberFormatException nfe) {
   // not an int so do what you need to do
}
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
That's true bobbit31!
0
 
LVL 5

Accepted Solution

by:
Calron earned 10 total points
Comment Utility
The way to solve this is to write a Document for the textfield. The concept is explained nicely in the java tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html#validation

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now