1

I have a form (JTable) for requesting firewall access. For the JTextField in which the user is to enter the sources, I would like to verify the input in real time as the user types. Valid inputs will be highlighted in green. Invalid should not be permitted.

The following Regex's are valid inputs:

private static final String IP_Address = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\d";//56.1.2.3
private static final String IP_WithMask = "(\\d{1,3}.){3}(\\d{1,3})(?:\\s+[255])(\\d{1,3}.){3}(\\d{1,3})"; //56.1.2.3 255.255.255.254
private static final String IP_CIDR = "(\\d{1,3}.){3}(\\d{1,3})(?:\\s*/)(\\d{1,3})"; //56.1.2.3/24 
private static final String IP_ADDRESS_Dash_Numeric_RANGE = "((\\d{1,3}.){3}(\\d{1,3})(?:\\s*-)(\\d{1,3}))";// 56.1.2.3-4
private static final String IP_ADDRESS_Dash_ADDRESS_RANGE = "((\\d{1,3}.){3}(\\d{1,3})(?:\\s*-\\s*)(\\d{1,3}.){3}(\\d{1,3}))";//56.1.2.3-56.1.2.5
private static final String IP_ADDRESS_To_Numeric_RANGE = "(\\d{1,3}.){3}(\\d{1,3})(?:\\s*[T|t][O|o]\\s*)(\\d{1,3})";//56.1.2.3 to 255
private static final String IP_ADDRESS_To_ADDRESS_RANGE = "((\\d{1,3}.){3}(\\d{1,3})(?:\\s*[T|t][O|o]\\s*)(\\d{1,3}.){3}(\\d{1,3}))";//56.1.2.3 to 56.1.3.5

In the JTextField, the user may type any permutation of these. I need to identify/verify each type to act upon them later.

I am using

public class MyVerifyer extends InputVerifier{}

and will apply this to my JTextfield with:

setInputVerifier();

Questions: 1. Can someone give me a regex that will capture all of these cases? 2. How do you match in real time as someone is typing. Perhaps attaching a TextListener to the JTextField?

I can provide more code if necessary.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
CoupFlu
  • 311
  • 4
  • 20
  • Please state your problem clearly. You already listed the regex patterns above, what oher regex pattern do you need? To capture which cases? And see http://stackoverflow.com/questions/2749521/how-to-validate-a-jtextfield for data validation in JTextField. – Wiktor Stribiżew Mar 04 '15 at 22:42
  • My problem is that I have multiple regex's that I'm trying to match as stated above. In the text field a person can type any of those like this : 56.1.2.3 56.1.2.4 255.255.255.254 56.1.2.7-9 56.5.1.1 to 56.5.1.7. In real time i need to be able to validate each input as one of the above cases. Since matcher only uses one regex, I think I need a single regex to look around while they are typing. – CoupFlu Mar 05 '15 at 14:45
  • I've never used them, but is Tokenizer and Lexer the best solution to this problem or is there something easier? – CoupFlu Mar 05 '15 at 15:20
  • I can only recommend to `join()` these patterns with "|", and then use to validate whatever input you have. – Wiktor Stribiżew Mar 05 '15 at 16:07

2 Answers2

0

I think you need to try something like this (not tested):

private static final String IP_Address = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\d";//56.1.2.3
private static final String IP_WithMask = "(\\d{1,3}.){3}(\\d{1,3})(?:\\s+[255])(\\d{1,3}.){3}(\\d{1,3})"; //56.1.2.3 255.255.255.254
private static final String IP_CIDR = "(\\d{1,3}.){3}(\\d{1,3})(?:\\s*/)(\\d{1,3})"; //56.1.2.3/24 
private static final String IP_ADDRESS_Dash_Numeric_RANGE = "((\\d{1,3}.){3}(\\d{1,3})(?:\\s*-)(\\d{1,3}))";// 56.1.2.3-4
private static final String IP_ADDRESS_Dash_ADDRESS_RANGE = "((\\d{1,3}.){3}(\\d{1,3})(?:\\s*-\\s*)(\\d{1,3}.){3}(\\d{1,3}))";//56.1.2.3-56.1.2.5
private static final String IP_ADDRESS_To_Numeric_RANGE = "(\\d{1,3}.){3}(\\d{1,3})(?:\\s*[T|t][O|o]\\s*)(\\d{1,3})";//56.1.2.3 to 255
private static final String IP_ADDRESS_To_ADDRESS_RANGE = "((\\d{1,3}.){3}(\\d{1,3})(?:\\s*[T|t][O|o]\\s*)(\\d{1,3}.){3}(\\d{1,3}))";//56.1.2.3 to 56.1.3.5

private static String regex_array = IP_Address + "|" + IP_WithMask + "|" + IP_CIDR + "|" + IP_ADDRESS_Dash_Numeric_RANGE + "|" + IP_ADDRESS_Dash_ADDRESS_RANGE + "|" + IP_ADDRESS_To_Numeric_RANGE + "|" + IP_ADDRESS_To_ADDRESS_RANGE;

public class MyInputVerifier extends InputVerifier {
@Override
public boolean verify(JComponent input) {
    String text = ((JTextField) input).getText().trim();
    if (text.isEmpty()) return false;
    if (text.matches(regex_array)) return false;

    return true;
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thats not quite what i'm trying to achieve. I want to verify the text while its being typed. At the moment when the entered text matches my regex, i want to highlight it. – CoupFlu Mar 06 '15 at 15:09
0

To achieve the result I was going for, I used a DocumentListener and assigned it to my TextField. Utilizing the insertUpdate method, I was able to verify the input in real time against a regex.

            @Override
        public void insertUpdate(DocumentEvent e) {
         //   System.out.println("insert:" + field.getText());
            m = p.matcher(this.field.getText());
            while (m.find()) {
                try {
                    String b = m.group();
                    System.out.println(b);
                    int index = field.getText().indexOf(b);
                    System.out.println("index=" + index);
                    int end = index + b.length();
                    System.out.println("end=" + end);
                    Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Valid_COLOR);

                    this.field.getHighlighter().addHighlight(index, end, painter);

                    System.out.println(">> " + b.substring(2, b.length() - 2));
                } catch (BadLocationException ex) {
                    System.out.println(ex);
                }
            }
        }
CoupFlu
  • 311
  • 4
  • 20