Solved

Validating a TextField in an Applet

Posted on 2002-03-21
4
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Expert Comment

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

Expert Comment

by:bobbit31
ID: 6886293
> 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
ID: 6886303
That's true bobbit31!
0
 
LVL 5

Accepted Solution

by:
Calron earned 10 total points
ID: 6894111
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

749 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