Count characters of an input ignoring the maskedinput

1

I have a script to advance the cursor to the next field of a form when the current field reaches its character limit (< a href="https://en.stackoverflow.com/a/111959/33193"> font ).

When I tried to put the maskedinput gave problem, because the mask counts as a character and it already jumps to the next field when I type the first number. How to solve?

I tried in this way , but without success.

    
asked by anonymous 12.02.2016 / 13:59

1 answer

2

You should use the complete helper of the plugin masked input, this helper allows you to perform a function when the mask is filled properly, follows a solution:

$("#cep").mask(
  "99999-999", {
    completed: function() {
      focusNext($(this))
    }
  });

$("#fax").mask("(99) 9999-9999");

function focusNext(el) {
  $(':input:eq(' + ($(':input').index(el) + 1) + ')').focus();
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.js"></script>
<label for="cep">CEP:</label>
<input type="text" id="cep" name="cep" data-length="9">

<label for="fax">Fax:</label>
<input type="tel" id="fax" name="fax">
    
12.02.2016 / 17:40