Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1998
  • Last Modified:

textfield validation

in a textfield, how can I validate a user fill in digits, charactors, "(", ")", and "-", not all other special symbols?

Thank you.
0
stanleyhuen
Asked:
stanleyhuen
  • 3
  • 3
  • 2
  • +3
1 Solution
 
tinchosCommented:
what you can do is accept the text entered and after that parse the string to see if there are invalid characters
0
 
objectsCommented:
Use a custom Document as discussed in your previous question.
0
 
ksivananthCommented:
Ya, custom document will cater you needs. In the insert string method, you can do the validations by checking the input string.

Siva
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jos010697Commented:
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
 
objectsCommented:
KeyListener won't work if you paste text into the field.
0
 
CEHJCommented:
ksivananth, please do not repeat other peoples answers - it does neither the questioner nor you any good.
0
 
jos010697Commented:
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
 
CEHJCommented:
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
 
jos010697Commented:
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
 
stanleyhuenAuthor Commented:
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
 
CEHJCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now