Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <input type="text" id="cnpj" value="P&G851223B24">
  <button type="button" id="validar">Validar</button>
  <div id="result"></div>
  <script src="https://code.jquery.com/jquery-2.1.4.js"></script>
</body>
</html>
 
function validarCnpj(cnpj) {
  return /^.{3,4}\d{6}.{3}$/.test(cnpj);
}
$(function() {
  var fieldCnpj = $('#cnpj');
  var button = $('#validar');
  var result = $('#result');
  button.on('click', function() {
    var cnpj = fieldCnpj.val().trim();
    var valid = validarCnpj(cnpj);
  
    result.html((valid ? 'CNPJ válido' : 'CNPJ inválido') + ': ' + cnpj);
  });
});
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers