wood burning stoves*
The moose likes Java in General and the fly likes help - with validating a TextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "help - with validating a TextField" Watch "help - with validating a TextField" New topic
Author

help - with validating a TextField

khella smith
Ranch Hand

Joined: May 06, 2002
Posts: 39
hi,
i have this code
and its works as it takes in a word from the text field and verifies it to see if it is a char
but i want it to take eg kelly smith at the same time so the space can also be verified is there a method for this ? that's for the text field to take only letters and space but not numbers i can't find any method or maybe did not know wat to look for in the api's can anyone help?
(edited by Cindy to put a meaningful Subject on this topic)
[ August 05, 2002: Message edited by: Cindy Glass ]
john guthrie
Ranch Hand

Joined: Aug 05, 2002
Posts: 124
if you're using java 1.4 you should look at the regular expressions stuff in java.util.regex
my perl skills are a bit rusty, but a regexp a la (([A-Za-z]* ?)+) might get you in the ballpark.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Well you COULD put a KeyListener on the TextField and if the user enters a numeric value, just consume() it, or change it to some other character.


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

Joined: Sep 08, 2001
Posts: 125
Not really an answer to your question, but you could also create your own method, and validate against an char array which contains all valid characters.
Erik
Femi Alla
Ranch Hand

Joined: Jul 05, 2002
Posts: 79
Dear Khella Smith,
I don't know if you are using AWT or Swing. Anyhow, with swing, this particular problem of yours gets easier the higher the version of JDK/SDK you are using.The InputVerifier class was introduced into Swing in JDK1.3 and could save one a lot of lines of code when validating a textfield. The APIs even have an example on it's usage, do check it out. Also JFormattedTextField was introduced in JDK1.4 and even has a more complete set of methods that support validating abitrary values in a JTextField.
It wouldn't be a bad idea for you to move up to a later version of the SDK (if you are using an old one, that is), moreso because it's................ free!.
Hope this helps.

Femi Alla.
SCJP2


SCJP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help - with validating a TextField