Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone. I am getting data via textbox but I want to restrict it. Namely, User can not enter character string or anything. I just want numeric characters between 100 and 999. Is it possible ?

String  coursecode = Integer.parseInt(txtCourseCode.getText());
if(coursecode<100 || coursecode>999)
           {
               JOptionPane.showMessageDialog(null,"Incorrect Course Code");
           }


this is a little part of my code.

Thanx.
Posted

Validation is the keyword.

No matter if you create your own or use a mechanism that is provided, you just need to validate before you move on:

Java
public class CustomVerifier extends InputVerifier {
    @Override
    public boolean verify(JComponent input) {
    String text = ((JTextField) input).getText();
    // verify
     return true; // if ok
    // return false; // when not ok   
    }
}

and add to the JTextfield:
Java
oTextField.setInputVerifier(new CustomVerifier());


You can also use a listener (propertyChange, focus) and validate there.

EDIT:
Do I need to mention that you need to make a custom JDialog for this? I guess not. Have fun.



EDIT 2:

Version 1 with a Listener:
Java
  textField.addKeyListener(new KeyAdapter() { 
  // Adapter is a "small version of a Listener", 
  // which does implement just the   needed functions
  @Override
  public void keyReleased(KeyEvent oEvent) { 
  // disgard the Event, we just need it to have a chance to react.
    String strContent = textField.getText();
    try { 
      int i = Integer.parseInt(strContent); // check by simple parsing
      if(i<=0 || i>=1000) { // number between 1 and 
        JOptionPane.showMessageDialog(null, "This number is <1 or   >999.");
      }
      // everything is ok
    } catch (NumberFormatException oException) {
      JOptionPane.showMessageDialog(null,"This is not even a number.");
    }
});
  }
}


Version2 with InputVerifyer:
Java
textField.setInputVerifier(new InputVerifier() {
  @Override
  public boolean verify(JComponent input) {
    String strContent = textField.getText();
    try { 
      int i = Integer.parseInt(strContent); // check by simple parsing
      if(i<=0 || i>=1000) { // number between 1 and 
        JOptionPane.showMessageDialog(null, "This number is <1 or >999.");
      }
      // everything is ok
    } catch (NumberFormatException oException) {
      JOptionPane.showMessageDialog(null, "This is not even a number.");
    }
    return true;
  }
});


I hope Edit2 helps. You need to learn that!
Please buy a book to get such information without knowing the keywords.
 
Share this answer
 
v5
Comments
FoxRoot 30-Jul-12 10:36am    
Thank you all. But these don't help me. If it helps I can send my class code which is totaly 20-30 lines ?
TorstenH. 30-Jul-12 12:26pm    
Follow this link: How to Make Dialogs - the Java Tutorials
It's simpler than you think.
FoxRoot 31-Jul-12 2:16am    
TorstenH dialog is OK. It is simple to do. However I can't control wheter the input is numeric or not.
TorstenH. 31-Jul-12 4:50am    
See Edit2 for code examples.
Think this does count as a solution. You can try using Regular Expressions to restrict the input to numbers only and once that's done you can have a bounds check on the values, which you already have done in your code apparently.
 
Share this answer
 
Comments
FoxRoot 30-Jul-12 9:48am    
Thanx for your comment. Isn't there any class or method to do that ?
I.explore.code 30-Jul-12 9:54am    
i can't recall off the top of my head, its been a while since I last touched Java. But you can write your own which is basically doing an Integer.parseInt(string) in try block and if it succeeds, return true otherwise from the catch block return false. Something like this: http://stackoverflow.com/questions/1102891/how-to-check-a-string-is-a-numeric-type-in-java. Other approach is always Regular Expressions.
FoxRoot 31-Jul-12 2:10am    
Thanx gladiatron.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900