How to validate an empty textField that can accepts only integers?
13,048
Solution 1
Typically, when you are validating user input in Java, you will want to check for both null and empty string values. To check String objects for equality, you must use the .equals() method (not the == operator). Thus, a check for an empty String value might look something like this:
if ( val == null || val.trim().equals( "" ) )
{
// handle empty String case
}
else
{
// handle non-empty String case
}
Hope this helps
Solution 2
if(textField_1.getText().isEmpty()){
JOptionPane.showMessageDialog(null, "enter text in textfield");
}
Comments
-
Anuradha over 1 year
I wrote like this; but it fails to integer only textfields
if(textField_1.getText().length()==0) JOptionPane.showMessageDialog(null, "enter text in textfield");
Please help...
-
Radu Murzea about 12 yearsindeed, if you check
String
s for equality, theequals
method is the way to go. But thelength
method is very ok to check if aString
is empty. Also, thegetText
method does not returnnull
Strings
. -
harry about 12 years@user1213523 i am glad that i could help, you can accept my answer by clicking on right sign just below the vote mark.
-
Muneeb Mirza about 10 yearsI think u had no need to add description of urself. Thanks for an easy solution though. :)