Count characters from an input ignoring the maskedinput

Asked

Viewed 477 times

1

I have a script to advance the cursor to the next field of a form when the current field reaches its character limit(source).

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

I tried to thus, but to no avail.

1 answer

2


You must use the complete helper of the plugin Masked input, this helper allows you to run 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>
<script src="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">

  • shooowww! Thank you very much msm, did not know this!

  • You are welcome, it is always a pleasure to share experience with the community, good luck in the execution of the project, abs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.