• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to validate text fields ?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mandar Puranik
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic