title: Android validate input while user inputs
body: <p>I have an <code>EditText</code> that the user must enters an phone number. The pattern for the phone number is: <code>^5[4][0-9]{10}$</code> First number is a <code>5</code>, second is a <code>4</code> followed by 10 digits.</p>
  
  <p>I've tried using the following <code>InputFilter</code></p>
  
  <pre><code>            InputFilter filter= new InputFilter() {
                  public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                      for (int i = start; i &lt; end; i++) {
                          String checkMe = String.valueOf(source.charAt(i));
  
                          Pattern pattern = Pattern.compile("^5[4][0-9]{10}$");
                          Matcher matcher = pattern.matcher(checkMe);
                          boolean valid = matcher.matches();
                          if(!valid){
                              return "";
                          }
                      }
                      return null;
                  }
              };
  </code></pre>
  
  <p>but this only matches the full number and I want to validate while the user is inputting digits.</p>
  
  <p>Imagine this situation:</p>
  
  <pre><code>User enters 6 -&gt; Does not match the initial digit of the pattern so EditText stays empty
  User enters 5 -&gt; Matches the first digit of the pattern so EditText text = 5
  User enters 3 -&gt; Does not match the second digit of the pattern so EditText text = 5
  User enters 4 -&gt; Matches the second digit of the pattern so EditText text = 54
  From now, if the user adds a digit, EditText will append that until the length of 10
  </code></pre>
  
  <p>Any idea how can I achieve this?</p>