How to create pattern with jQuery Validation Plugin?

1

I need to create a input mask (pattern) for the CPF type 000.000.000-00 and for CNPJ 00.000.000 / 0000-00 via the jQuery Validation plugin. What would be the regex to be creating this validation?

Note: CPF and CNPJ will be two different inputs.

Thank you.

    
asked by anonymous 01.12.2016 / 19:32

1 answer

2

For this you can use the jQuery Mask .

$(document).ready(function(){
  $('.cpf').mask('000.000.000-00');
  $('.cnpj').mask('00.000.000/0000-00');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><scriptsrc="https://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>

CPF :<input type="text" class="cpf">
<br/>
CNPJ :<input type="text" class="cnpj">

Note

  • Remembering that so it does not check if it's valid content, just put it in this format.

Edit

The REGEXs to validate CPF and CNPJ could be

  • CPF: \d{3}\.\d{3}\.\d{3}-\d{2}
  • CNPJ: \d\d\.\d{3}\.\d{3}-\d{4}-\d\d

See the REGEX101 .

    
02.12.2016 / 13:28