?
Solved

textfield validation

Posted on 2003-02-25
11
Medium Priority
?
1,995 Views
Last Modified: 2012-05-04
in a textfield, how can I validate a user fill in digits, charactors, "(", ")", and "-", not all other special symbols?

Thank you.
0
Comment
Question by:stanleyhuen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 8022332
what you can do is accept the text entered and after that parse the string to see if there are invalid characters
0
 
LVL 92

Expert Comment

by:objects
ID: 8022406
Use a custom Document as discussed in your previous question.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 8022964
Ya, custom document will cater you needs. In the insert string method, you can do the validations by checking the input string.

Siva
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 4

Expert Comment

by:jos010697
ID: 8023734
How about this:

field.addKeyListener(new KeyAdapter() {
   public void keyPressed(KeyEvent e) {

      char c= e.getKeyChar();

      if (!isOK(c))              
         e.consume();
   }
});

Your function 'isOK()' checks whether or not character 'c' is acceptible.

kind regards
0
 
LVL 92

Expert Comment

by:objects
ID: 8023798
KeyListener won't work if you paste text into the field.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8024577
ksivananth, please do not repeat other peoples answers - it does neither the questioner nor you any good.
0
 
LVL 4

Expert Comment

by:jos010697
ID: 8024758
obejcts wrote: KeyListener won't work if you paste text into the field.

Ah, yes you're right, but together with the KeyEventListener (see my previous reply), a simple TextEventListener would do the job: the KeyEventListener controls the keystrokes while the TextEventListener would control the characters currently present in the TextField.

kind regards
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8024829
stanleyhuen, exactly the same principle applies to limiting which characters are input as to how many are input, i.e. using a custom document in the input field. objects has already answered that at http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20507874.html, which you should now accept and close, in addition to this one.
0
 
LVL 4

Expert Comment

by:jos010697
ID: 8025012
Oops, I didn't realize that the OP was asking similar questions. objects' answer is way more elegant than my quick hack; I'll refrain from further discussion in this topic.

kind regards
0
 

Author Comment

by:stanleyhuen
ID: 8051390
Thanks all.

In fact, I think this question is different from that question (http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20507874.html),
In this question, I just want to check only digits, charactors and "(", ")" and "_" in a String, if there are other special charactors, eg, &, $, %, ^ or ! etc, then the method returns false.

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 800 total points
ID: 8051470
Well, it's just that the principle is the same. Here's the code anyway. Just set your component's document to an instance of this class:

class AlphaNumericDocument extends PlainDocument
{
    public AlphaNumericDocument()
    {
         super();
    }

    public void insertString(int offset, String s, AttributeSet attributeSet)
         throws BadLocationException
    {
         try
         {
              //Integer.parseInt(getText(0, offset) + s + getText(offset, getLength()-offset));
              char entered = s.charAt(0);
              if (Character.isDigit(entered) ||
                  Character.isLetter(entered) ||
                  (entered == ',') ||
                  (entered == '_'))
                  super.insertString(offset, s, attributeSet);
         }
         catch (Exception ex)
         {
              Toolkit.getDefaultToolkit().beep();
         }

    }
}
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question