-1

I have a text field (for name )that i want to prevent user to enter number, Or empty!

I try all of this, But there is some infirmity in it:

public boolean nameControl(String str) {
    if (haveDigit(str)) return false;        // works corrctly
    else if (str.isEmpty()) return false;     // Not work
    else if (str.length() == 0) return false;    // not work
    return true;
}
Sajad
  • 2,273
  • 11
  • 49
  • 92
  • 2
    How or where is this code being called? [SSCCE](http://sscce.org)? DocumentFilters work great for this, and if you search this site, you'll find answers for this type of problem using this tool. Also, please consider making your questions more complete, so that we don't have to keep dragging information out of you all the time. – Hovercraft Full Of Eels Sep 22 '13 at 21:56
  • 1
    Or `InputVerifier`, seen [here](http://stackoverflow.com/a/11818183/230513). – trashgod Sep 22 '13 at 22:22

1 Answers1

4

As described in How to Use the Focus Subsystem: Validating Input, you may be looking for InputVerifier. There's a related example here, among others.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045