File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "help - with validating a TextField" Watch "help - with validating a TextField" New topic

help - with validating a TextField

khella smith
Ranch Hand

Joined: May 06, 2002
Posts: 39
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"

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.
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.

I agree. Here's the link:
subject: help - with validating a TextField
jQuery in Action, 3rd edition