• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validation for Textfield

 
RaviSingh Kumar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Want to validate the textfield as soon as some invalid text is entered.
for e.g. If is EmpID field, error message is shown as soon as some non-integer
value is entered.

I had tried it to validate once the whole form is submitted,but unable to provide solution for the above situation.

Please provide appropriate steps for this. The application is in Swing
 
Campbell Ritchie
Sheriff
Pie
Posts: 49845
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The application might be in Swing, but the entered text is a String. Maybe a regular expression and matcher would be a good next step.
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you need is a document listener. With it you can get notifications for all the changes in the document and you can execute your validation code there. Heres one example which calls the validate() which will contain the actual validation code.

 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a simple TextField, using KeyListener might be a little easier.

Of course, it depends how complicated your validation requires.
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by RaviSingh Kumar:
Want to validate the textfield as soon as some invalid text is entered.
for e.g. If is EmpID field, error message is shown as soon as some non-integer value is entered.

I had tried it to validate once the whole form is submitted,but unable to provide solution for the above situation.

Please provide appropriate steps for this. The application is in Swing

This is what JFormattedTextField is for, if you can presume java 1.4 or better. Alas, JFormattedTextField isn't all that well documented.

(Calling DefaultFormatter.setAllowsInvalid(false) would prevent the user from typing invalid input at all, which is a bit different from allowing it but showing an error message.)

[edit: JFormattedTextField came with 1.4, not 1.5]
[ October 19, 2007: Message edited by: Brian Cole ]
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Cole:
Calling DefaultFormatter.setAllowsInvalid(false) would prevent the user from typing invalid input at all, which is a bit different from allowing it but showing an error message.


I was looking for that one , thanks Brian.
 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want a Numeric TextField
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use a JSpinner?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic