• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Check the Characters in Text Box!

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at mindy's response. It sounds good to me.
 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic