-1

Working on a project with javafx and I'm having a minor hitch. I want my textfield to accept only digits, 8 or 11 in length. Here's my code:

if(!txtPhone.getText().matches(.....) && (txtPhone.getText().length != 8 || txtPhone.getText(). length != 11){
System.out.print("Please enter a valid phone number");
}
  • You want the error message to be displayed if one of the criterions is satisfied, right? Currently the expression in the brackets always evaluates to `true` (after fixing the syntax errors): The text length is always different to 8, 11 or both. This makes the expression logically equivalent to `!txtPhone.getText().matches(.....)`. You probably intended to use `String text = texttPhone.getText(); if (!(((text.length() == 11) || (text.length() == 8)) && text.matches("\\d*")))` (Note that I reordered the conditions a bit, since comparing the string lenght takes less time than matching the regex.) – fabian Nov 18 '19 at 17:17
  • Using de morgan you could write the condition in the above comment to `!text.matches("\\d*") || (text.length() != 8 && text.length() != 11)`, i.e. it's an error, if the text doesn't contain only digits or if the text lenght is different to both allowed lenghts. – fabian Nov 18 '19 at 17:20

2 Answers2

1

The task involves two stages:

  • You must first create a text box that accepts digits only (up to 11 maximum).
  • Second, you have to customize the user input according to your criteria (8 or 11 digits)

TextFormatter is used to solve the problem. A UnaryOperator must be passed to it to filter user input only by numbers and StringConverter to validate user input.

This is an example implementation:

UnaryOperator<TextFormatter.Change> filter = change -> {
    if(change.getControlNewText().matches("\\d{0,11}")) {
        return change;
    }

    return null;
};

StringConverter<String> converter = new StringConverter<String>() {
    @Override
    public String toString(String s) {
        if(s == null || s.isBlank()) return "";

        if(s.matches("\\d{8}|\\d{11}")) {
            return s;
        }

        return "";
    }

    @Override
    public String fromString(String s) {
        if(s == null || s.isBlank()) return "";

        if(s.matches("\\d{8}|\\d{11}")) {
            return s;
        }

        throw new RuntimeException("Converter error");
    }
};

textField.setTextFormatter(new TextFormatter<>(converter, null, filter));
mr mcwolf
  • 2,574
  • 2
  • 14
  • 27
  • Thanks @mr mcwolf. How do I fit these into the if statement above, in a line of code? – John Omage Nov 18 '19 at 22:05
  • @JohnOmage, the `if` operator above is practically useless. It simply prints text in the console. And this in the context of the GUI only makes sense as a debug output. If so, paste tracking messages into the body of `StringConverter::fromString`. – mr mcwolf Nov 19 '19 at 07:15
  • It seems more likely that you want to notify the user that he or she did not enter the phone number before proceeding. Therefore, there are several possible approaches: disable the OK button, disable the dialog closing with the corresponding message, or simply show a notification (icon, text ...) that the field is blank. How exactly to do this is impossible to say since there is no additional information. – mr mcwolf Nov 19 '19 at 07:15
1

Regular Expression can be used to create custom validations.

if (txtPhone.getText().matches("\\d{8}|\\d{11}") {

    System.out.println("Its Valid Number");
    //return true;
}else {

    System.out.println("Invalid Input..!");
    //return false;
}

You can learn and check about Regular Expressions Here

Ahamed Safnaj
  • 611
  • 1
  • 6
  • 18