CPF CNPJ Campo

no mesmo campo

by Rapha Souza

HTML

<link rel="stylesheet" href="https://getbootstrap.com/dist/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script src="https://igorescobar.github.io/jQuery-Mask-Plugin/js/bootstrap.min.js"></script>
<script src="https://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>
<div class="col-xs-6 col-sm-6 col-md-4">
  <div class="form-group">
    <label>CPF/CNPJ</label>
    <input type="text" name="cpfCnpj" id="cpfCnpj" class="form-control">
  </div>
</div>

JavaScript

// Mascara de CPF e CNPJ
var CpfCnpjMaskBehavior = function (val) {
			return val.replace(/\D/g, '').length <= 11 ? '000.000.000-009' : '00.000.000/0000-00';
		},
    cpfCnpjpOptions = {
    	onKeyPress: function(val, e, field, options) {
      	field.mask(CpfCnpjMaskBehavior.apply({}, arguments), options);
      }
    };

$(function() {
	$(':input[name=cpfCnpj]').mask(CpfCnpjMaskBehavior, cpfCnpjpOptions);
})