0

Textfield that not accept any character except number [0-9] and its length also, in my case it is 11 but after adding {11} in regex expression that textfield doesn't accept any thing. Please help!

Code here:

public class NumberField extends JFXTextField {

        @Override
        public void replaceText(int i, int i1, String string){

            if (string.matches("[0-9]{11}") || string.isEmpty()) //{11} is length of number
                super.replaceText(i , i1 , string);

        }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Sorry for proofreading.Textfield that not accept any character except number [0-9] and takes specific length input, in my case it is 11 but after adding {11} in regex expression "the" textfield doesn't accept any thing. Please help! –  Mar 25 '20 at 20:52

1 Answers1

0
public class ContactField extends JFXTextField {

    /*
     *
     * i is current length / value
     * i1 is previous length / value
     * string is value / char i-e currently keytyped
     *
     * Here:
     *
     * string.matches[0-9], it matches single current key typed value to the regex expression(one at a time)
     * string.empty() is used for backspace key
     */
    @Override
    public void replaceText(int i, int i1, String string) {
         if ((string.matches("[0-9]") || string.isEmpty()) && i <= 10) //for validating numbers length start from 0;
             super.replaceText(i, i1, string);
         }

    /*
     * It maintains prev and current values when selected char( by arrows keys like from middle) is deleted;
     *
     */
    @Override
    public void replaceSelection(String replacement) {
        super.replaceSelection(replacement);
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
maazakn
  • 384
  • 2
  • 10