• Post Reply Bookmark Topic Watch Topic
  • New Topic

A-Z and 0-9 for textfield validation  RSS feed

 
Herbert Kornfeld
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What is the best way to do a textfield validation - the rules I need are just A-Z upper and lower case allowed and 0-9 - no funny characters like#^% etc.
Many Thanks
 
indian greenhorn
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add KeyListener to the textfield and call keyPressed() or keyTyped() method. Eg,
public void KeyTyped(KeyEvent ke)
{
System.out.println(ke.getKeyCode());
}
It will printout the codes for the keys typed on the screen. You first get the code for the #,^ etc and by if condition check for their presence.
that is if(ke.getKeyCode()==100) inside the keyTyped() block. If it equals to the key code then give the alert message you want to display.
regards,
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Herbert,
If you haven't already seen them, I think the following two sections from the Java Tutorial will be of help:
http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html
http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html
Hope this helps you.
Good Luck,
Avi.
 
Herbert Kornfeld
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
Both replies are very helpful but does anyone have sample code on how to use the JFormattedTextField.
I have looked at the API and I can't make much sense of it. And the link doesn't have any sample code, which always helps.
Thanks Again!
 
Tim Troy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the RegexPatternFormatter class at http://examples.oreilly.com/jswing2/code/#ch20.
Here is a code snippet where I used it to set the column editor in a table.
// Pattern is any number of digits or upper case letters, 0 - 30 in length. Allowing 0 makes editing the column easier at the expense of possibly not having a value entered. This should be caught prior to saving
 
Tim Troy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some links that may help you using regular expressions with Java.
JDC Tech Tip
Java Tutorial
JDC Article
JFC Article
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!