Allow only dash number (hyphen), bar and dot in inpult

Asked

Viewed 410 times

0

I am trying to make a CPF & CNPJ field with mask, but I want to allow in the field only number, dash (hyphen), bar & dot;

and want to put to Mask works like this when I had 14 digit (cpf) it do the mask of the same and 18 (CNPJ).

NOTE: SORRY, I AM TERRIBLE IN PORTUGUESE.

function mask(InputMask, DocMask){
  var m = DocMask.value.length;
  var MSay = InputMask.substring(0,1);
  var MText = InputMask.substring(m)
  if (MText.substring(0,1) != MSay){
    DocMask.value += MText.substring(0,1);
  }
}
$("input[name='doc']").on('blur', function(){
  var doc = $(this).val();
  $.get('./functions/filter.php?doc=' + doc,function(data){
    $('#doc').html(data);
  });
});
jQuery('.doc').keyup(function () { 
    this.value = this.value.replace(/[^0-9.-]/g,'');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input class="input100 doc" type="text" name="doc" autocomplete="off" required="required" placeholder="CPF/CNPJ" minlength="14" maxlength="18" OnKeyPress="mask('###.###.###-##', this)">

  • And the hyphen of the CPF?

  • 1

    also, thanks for remembering... :)

  • IS NOT DUPLICATE...

  • Dear Ewerson, there is much on the subject, as listed above. If you want to reopen the question, you can click the "reopen" link just below the question.

No answers

Browser other questions tagged

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