This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of TensorFlow 2.0 in Action and have Thushan Ganegedara on-line!
See this thread for details.
Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

A-Z and 0-9 for textfield validation

 
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
 
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,
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • 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!
 
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
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic