PHP code example of renatomagalhaes / validador-cnpj

1. Go to this page and download the library: Download renatomagalhaes/validador-cnpj library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

renatomagalhaes / validador-cnpj example snippets


use Unoweb\Cnpj\Cnpj;

// Validação simples (Retorna bool)
Cnpj::validate('JR.32M.XJG/0001-69'); // true
Cnpj::validate('00.000.000/0001-91'); // true (Legado)

// O validador normaliza a entrada automaticamente (uppercase e sem máscara)
Cnpj::validate('jr32mxjg000169');     // true

// app/Rules/CnpjRule.php
namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Unoweb\Cnpj\Cnpj;

class CnpjRule implements ValidationRule
{
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (!Cnpj::validate((string) $value)) {
            $fail('O campo :attribute não é um CNPJ válido.');
        }
    }
}

$request->validate([
    'documento' => ['