Showing posts with label Validation. Show all posts
Showing posts with label Validation. Show all posts

Friday, August 7, 2015

JavaFx - Validating Numeric Only Text Field

Earlier, I posted about validating IP in a Text Field. Thought of sharing information about Numeric Only validation too. So, its pretty simple. Here are some lines of codes

1. @FXML
    private TextField port;

2.  if (!validatePort(port.getText())) {
                showDialog("Invalid Port");
            }

3.  public boolean validatePort(final String port) {
        Pattern pattern;
        Matcher matcher;
        String PORT_PATTERN
                = "^([0-9]+)$";
        pattern = Pattern.compile(PORT_PATTERN);
        matcher = pattern.matcher(port);
        return matcher.matches();
    }

JavaFx - Validating Text Field with IP

Recently, I happen to come across IP validation problem in JavaFX code. So, I googled and googled. In the end, I found the relevant information. Here are some lines of codes, which is useful. Use it when you need it.

1. @FXML
    private TextField ip;

2.  if (!validateIP(ip.getText())) {
                showDialog("Invalid IP Address");
            }

3.  public boolean validateIP(final String ip) {
        Pattern pattern;
        Matcher matcher;
        String IPADDRESS_PATTERN
                = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
        pattern = Pattern.compile(IPADDRESS_PATTERN);
        matcher = pattern.matcher(ip);
        return matcher.matches();
    }