Solved

Validating a TextField in an Applet

Posted on 2002-03-21
4
292 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
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 107
Checkbox and ListView in Android Layout 4 65
servlet  URL Rewriting 1 36
mysql jsp example issue 32 36
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

773 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