• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField  RSS feed

 
Frank Hale
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm looking for an example of how to limit a JTextField so that you can put a phone number in it. I'm not sure what you call fields like that but I only want to have the user be able to enter a phone number.
Thanks,
Frank
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sort of field is often known as "formatted" or "validated", depending on whether the checking is done for every keystroke or only when the field is moved out of/submitted.
However, I have almost a personal crusade against such fields. I absolutely hate fields which limit the input to what the programmer or designer thought might be entered, especially if they are mandatory on the form.
I personally believe that thousands of companies are losing e-commerce sales every day, and probably don't even know about it, all because of needless validation on form fields. About half the on-line forms I see have some form of stupid validation :- either they enforce the US-style 3-3-4 phone numbers, require that I select a "state" from a list (or only allow two characters), only allow numbers in the "zip code" and so on.
Lots of Us-designed forms won't even work for a Canadian customer, let alone one from elsewhere in the world.
Whenever you think about putting formatting or validation on an input field - think twice; No. Think three times and take a cold shower. You don't need it. Let the user decide what is a valid address or phone number.
Sorry for the rant.
 
Frank Hale
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I still need a formatted JTextField because the users of my software are dumb people (as most users are). I have to force them to enter the correct data because I cannot trust them to enter in the required information. And this is not for a web site as you may have thought from your rant.
Frank
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Hale-
OK, assuming that it is a good/necessary idea in this case, check out Sun's Swing tutorial, specifically Creating a Validated Text Field. I've only skimmed it, but it seems to address your problem. Also, the book "Java Pitfalls" by Daconta, Monk and others has a section on this - worth checking out. Note that some things can be validated immediately (e.g. if the user types '@' you can reject it right away) while others can't be validated until the user hits "Enter" or the field loses focus (e.g. if there is only one digit, more are probably needed). Good luck. For extra fun, try to get Mr. Carver to be one of your beta testers when you think you're done.
 
Frank Hale
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you I think this is just what I need.
Frank
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!