0

I'm testing a new form need regex from 1 to 10k numbers only in java

// Start Numbers Section
                String number = FNumber.getText();
                
                if(number.matches("^([0-9][0-9]{1,3}|10000)$")) {
                    System.out.println("");
                } else {
                    JOptionPane.showMessageDialog(contentPane,"Please enter number from 1 to 10,000 only!","Alert Message",JOptionPane.ERROR_MESSAGE);
                    FNumber.setText("");
                }
                // End Numbers Section
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • 1
    Why write it like that? Parse as an integer and then do a simple range-check. – user16632363 Nov 24 '21 at 13:46
  • Use a `JFormattedTextField` or `JSpinner`. Each component can be configured to allow you to only enter integer values. Read the [Swing tutorial](https://docs.oracle.com/javase/tutorial/uiswing/TOC.html) for working examples of each. – camickr Nov 24 '21 at 14:23
  • Note, by the way, that as written, your regex allows 0, which you should refuse., and rejects single-digit numbers, which you should accept. – user16632363 Nov 24 '21 at 20:05

0 Answers0