wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes How to validate text fields ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to validate text fields ?" Watch "How to validate text fields ?" New topic
Author

How to validate text fields ?

Mandar Puranik
Ranch Hand

Joined: Jan 08, 2003
Posts: 35
Hi,
Here is my problem.
I have 3 text fields in a dialog box.
I want the user to compulsarily enter some values in it. The fields shouldnt be blank. So when a user clicks 'OK' button, I want to validate if all the textfields contain some values or not.
I tried to do gettext for all textfields and checking the length for zero, or the if that string is null or not.
But this doesnt work.
Any advice ?
Thanks
Mandar


To Bug is Human,<br />To Debug Divine... :-))
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
checking for null and the text length is a valid way to achieve what you want. otherwise you wouldn't be able to retrieve any values from the fields using getText().
Thus, I suppose you have some bug in your code that makes the getText() method return some strange values. Could you paste the appropriate part of your code? (What exactly _is_ returned by getText() either when you typed values in the fields, or when you didn't.)
On second thought: When the user clicks "OK" the dialog box normally closes and it would be to late to check for values. So what you really (should) want to do is: Disable the OK-button unless all fields are filled. You do this by attaching a key listener or a document listener to the text fields (or their documents, respectively). Inside these listeners you can check the length of the value (using getText()) and enable or disable the button accordingly. If you are using a factory method of JOptionPane to open the dialog you won't be able to implement this solution (for what I know). You will have to create a JOptionPane using the constructor. (or use a JDialog)
chantal
[ January 09, 2003: Message edited by: Chantal Ackermann ]
gautham kasinath
Ranch Hand

Joined: Dec 01, 2000
Posts: 583
well to supplement the above post..
you can have your validation part in the actionperformed method as the first thing.
say :

if you can send the code we can probably work on it better.
And for validation, do complex validation in a simple and effective way using the regEX.
Lupo


"In the country of the blind, the one eyed man is the King"
Gautham Kasinath CV at : http://www.geocities.com/gkasinath
Mandar Puranik
Ranch Hand

Joined: Jan 08, 2003
Posts: 35
Thanks Chantal and Gautam.
Well, here is the code in my application.
public void actionPerformed(ActionEvent ae)
{
Object obj = ae.getSource();
if (obj == jbtnOK)
{ String strBaseStr = baseParam.getText();
String strStartIndex = startParam.getText();
String strEndIndex = endParam.getText();
String mainString = "Substring(" + StrBaseStr + ", " + strStartIndex + ", " + strEndIndex + ")";
//Do some other operation with the mainString.
//I am appending the mainString to another //JTextArea.
this.dispose();
}
else {if (obj == jbtnCancel)
{this.dispose();
}
}
}
I have removed the validation code, so as right now my application should run. But I want to add it.
Thanks in advance
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
I think you need to trim it. Spaces will cause the length to be non-zero. But if you trim it first and check for an empty string it should be ok.
if ( textfield.getText().trim().equals("") )
//do something
 
Consider Paul's rocket mass heater.
 
subject: How to validate text fields ?
 
Similar Threads
retaining value in java script
Issue with Vaanilla Struts 2/Hibernate CRUD example
persistent values in jsp
Tag library usage
INSERT