This week's giveaways are in the MongoDB and Jobs Discussion forums. We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line! See this thread and this one for details.
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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.