aspose file tools*
The moose likes Beginning Java and the fly likes Check the Characters in Text Box! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Check the Characters in Text Box!" Watch "Check the Characters in Text Box!" New topic
Author

Check the Characters in Text Box!

Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
I have one text box which allows user to enter characters string as well as interger & float values. I don't want user to allow character string.
Thanks,
Angela
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
One option is to put a KeyListener on the TextField and listen for KeyEvents. If the user enters anything other than the numbers VK_0 through VK_9 and VK_DECIMAL and maybe a VK_MINUS, you consume() the event so it looks like they didn't type anything.
Another option is to have a Submit button and when they submit the form, check the contents of the TextFeild and if it contains characters, send back an error message telling them what they are doing wrong.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
This example just show you how to control the user input, user can type in 0-9 in the text field only:
public void keyTyped( KeyEvent e)
{
char ch = e.getKeyChar();
System.out.println("key name: " + ch);
if ( ch < '0' | | ch > '9' ) // not a digit
e.consume();
}
Hope this helps!
You need to register your textfield with a keyListener first.
[This message has been edited by Mindy Wu (edited June 19, 2001).]
Conrad Kirby
Ranch Hand

Joined: Jun 17, 2001
Posts: 178
Yea, that was what my question was: do you want the user to not be able to type any characters besides number characters, or do you just want to ignore non-number characters when they submit?
(I can answer the second)
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Thanks all,
Conrad,
I want user to type character, but when they type non number character, I want to display the message that user can't enter string value. USER CAN ONLY ENTER NUMBERS. I know how to display dialog. But I don't know what condition I should put to display message when User enter non-number characters.
Thanks,
Angela
Originally posted by Conrad Kirby:
Yea, that was what my question was: do you want the user to not be able to type any characters besides number characters, or do you just want to ignore non-number characters when they submit?
(I can answer the second)

Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428

Cindy,
How I can check whether TextField has non numbers character or not...
Thanks
Angela
Originally posted by Cindy Glass:
One option is to put a KeyListener on the TextField and listen for KeyEvents. If the user enters anything other than the numbers VK_0 through VK_9 and VK_DECIMAL and maybe a VK_MINUS, you consume() the event so it looks like they didn't type anything.
Another option is to have a Submit button and when they submit the form, check the contents of the TextFeild and if it contains characters, send back an error message telling them what they are doing wrong.

Conrad Kirby
Ranch Hand

Joined: Jun 17, 2001
Posts: 178
Look at mindy's response. It sounds good to me.
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
Hi Angela,
check out the following link at sun: http://web2.java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html
I believe this will help you out!
Cheers!
Mindy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Check the Characters in Text Box!
 
Similar Threads
Ajax Technology
String to Float
Problem with linefeed
Javascript
Show default text in PasswordTextBox.