next-CNPJ
1.1.0
dotnet add package next-CNPJ --version 1.1.0
NuGet\Install-Package next-CNPJ -Version 1.1.0
<PackageReference Include="next-CNPJ" Version="1.1.0" />
<PackageVersion Include="next-CNPJ" Version="1.1.0" />
<PackageReference Include="next-CNPJ" />
paket add next-CNPJ --version 1.1.0
#r "nuget: next-CNPJ, 1.1.0"
#:package next-CNPJ@1.1.0
#addin nuget:?package=next-CNPJ&version=1.1.0
#tool nuget:?package=next-CNPJ&version=1.1.0
next-CNPJ
.NET library for robust CNPJ validation with support for the new alphanumeric format according to NTC 2025.001.
π About
The next-CNPJ library provides complete CNPJ (Cadastro Nacional da Pessoa JurΓdica) validation, including support for the new alphanumeric format that will come into effect. It implements all NTC 2025.001 rules, including calculation and validation of check digits, automatic normalization, and format identification.
β¨ Features
- β Numeric CNPJ validation (traditional format)
- β Alphanumeric CNPJ validation (new NTC 2025.001 format)
- β Automatic check digit calculation and validation
- β Automatic normalization (removes formatting: dots, slashes, hyphens)
- β Automatic format identification (numeric or alphanumeric)
- β Excluded letters configuration (I, O, U, Q, F according to ENCAT)
- β Support for CNPJ with or without formatting
- β Automatic lowercase to uppercase conversion
- β Rejection of CNPJ with all identical characters (e.g., 00000000000000)
- β Detailed results with descriptive error messages
π Installation
Install the package via NuGet:
dotnet add package next-CNPJ
Or via Package Manager Console:
Install-Package next-CNPJ
π Basic Usage
Simple Validation
using next_CNPJ.Core.Services;
var validator = new CnpjValidator();
// Simple validation - returns true/false
bool isValid = validator.IsValid("11222333000181");
Console.WriteLine($"Valid CNPJ: {isValid}"); // true
Detailed Validation
using next_CNPJ.Core.Services;
using next_CNPJ.Core.Domain;
var validator = new CnpjValidator();
// Detailed validation - returns complete information
var result = validator.Validate("11222333000181");
if (result.IsValid)
{
Console.WriteLine($"Valid CNPJ!");
Console.WriteLine($"Format: {result.Format}"); // Numeric
Console.WriteLine($"Normalized CNPJ: {result.NormalizedCnpj}"); // 11222333000181
}
else
{
Console.WriteLine($"Error: {result.ErrorMessage}");
}
Validation with Formatting
The library accepts CNPJ with or without formatting:
var validator = new CnpjValidator();
// All these forms are accepted:
validator.IsValid("11222333000181"); // Without formatting
validator.IsValid("11.222.333/0001-81"); // With traditional formatting
validator.IsValid("12.ABC.345/01DE-35"); // Alphanumeric format with formatting
π€ Alphanumeric CNPJ
Alphanumeric CNPJ Validation
The new alphanumeric format allows letters in the root (positions 1-8) or in the order (positions 9-12):
var validator = new CnpjValidator();
// Example of valid alphanumeric CNPJ
var result = validator.Validate("12ABC34501DE35");
if (result.IsValid)
{
Console.WriteLine($"Format: {result.Format}"); // Alphanumeric
Console.WriteLine($"Normalized CNPJ: {result.NormalizedCnpj}"); // 12ABC34501DE35
}
Excluded Letters
By default, the letters I, O, U, Q, F are excluded according to ENCAT technical specification:
var validator = new CnpjValidator();
// CNPJ with excluded letter (I) - invalid
var result = validator.Validate("12IBC34501DE35");
Console.WriteLine(result.IsValid); // false
Console.WriteLine(result.ErrorMessage); // "The root segment contains the letter 'I' which is not allowed. Excluded letters: I, O, U, Q, F."
Custom Configuration
You can customize excluded letters or allow all letters:
using next_CNPJ.Core.Domain;
var config = new CnpjConfiguration
{
ExcludedLetters = new[] { 'I', 'O' }, // Only I and O excluded
AllowExcludedLetters = false
};
var validator = new CnpjValidator();
var result = validator.Validate("12IBC34501DE35", config);
To allow all letters (including normally excluded ones):
var config = new CnpjConfiguration
{
AllowExcludedLetters = true // Allows all letters
};
var result = validator.Validate("12IBC34501DE35", config);
Invalid CNPJ Patterns
The library rejects CNPJs with all identical characters (including all zeros):
var validator = new CnpjValidator();
// CNPJ with all zeros - invalid
var result = validator.Validate("00000000000000");
Console.WriteLine(result.IsValid); // false
Console.WriteLine(result.ErrorMessage); // "CNPJ invΓ‘lido: todos os caracteres sΓ£o iguais."
// CNPJ with all same digits - invalid
var result2 = validator.Validate("11111111111111");
Console.WriteLine(result2.IsValid); // false
Console.WriteLine(result2.ErrorMessage); // "CNPJ invΓ‘lido: todos os caracteres sΓ£o iguais."
π Format Identification
You can identify the CNPJ format before validating:
using next_CNPJ.Core.Services;
var identifier = new CnpjFormatIdentifier();
// Identify format
var format = identifier.IdentifyFormat("12ABC34501DE35");
Console.WriteLine(format); // Alphanumeric
// Quick checks
bool isAlphanumeric = identifier.IsAlphanumeric("12ABC34501DE35"); // true
bool isNumeric = identifier.IsNumeric("11222333000181"); // true
π API Reference
ICnpjValidator
Main interface for CNPJ validation.
Methods
bool IsValid(string? cnpj, CnpjConfiguration? config = null)- Validates a CNPJ and returns
trueif valid,falseotherwise.
- Validates a CNPJ and returns
CnpjValidationResult Validate(string? cnpj, CnpjConfiguration? config = null)- Validates a CNPJ and returns a
CnpjValidationResultobject with detailed information.
- Validates a CNPJ and returns a
CnpjValidationResult
Validation result with the following properties:
bool IsValid- Indicates if the CNPJ is validCnpjFormat Format- Identified format (Numeric or Alphanumeric)string? ErrorMessage- Error message (null if valid)string NormalizedCnpj- Normalized CNPJ (without formatting)
ICnpjFormatIdentifier
Interface for CNPJ format identification.
Methods
CnpjFormat IdentifyFormat(string? cnpj)- Identifies the CNPJ formatbool IsAlphanumeric(string? cnpj)- Checks if it is alphanumericbool IsNumeric(string? cnpj)- Checks if it is numeric
CnpjConfiguration
Configuration for custom validation.
Properties
char[] ExcludedLetters- Letters that should not be accepted (default: I, O, U, Q, F)bool AllowExcludedLetters- Allows excluded letters even if they are in the list (default: false)
π‘ Use Cases
1. Web Form Validation
public class CnpjValidationService
{
private readonly ICnpjValidator _validator;
public CnpjValidationService(ICnpjValidator validator)
{
_validator = validator;
}
public ValidationResult ValidateUserInput(string cnpj)
{
var result = _validator.Validate(cnpj);
if (!result.IsValid)
{
return new ValidationResult
{
IsValid = false,
ErrorMessage = result.ErrorMessage
};
}
return new ValidationResult
{
IsValid = true,
NormalizedCnpj = result.NormalizedCnpj,
Format = result.Format.ToString()
};
}
}
2. Batch Processing
public void ValidateBatch(IEnumerable<string> cnpjList)
{
var validator = new CnpjValidator();
var results = new List<CnpjValidationResult>();
foreach (var cnpj in cnpjList)
{
var result = validator.Validate(cnpj);
results.Add(result);
if (result.IsValid)
{
Console.WriteLine($"β {cnpj} - {result.Format}");
}
else
{
Console.WriteLine($"β {cnpj} - {result.ErrorMessage}");
}
}
}
3. API Integration
[HttpPost("validate")]
public IActionResult ValidateCnpj([FromBody] CnpjRequest request)
{
var validator = new CnpjValidator();
var result = validator.Validate(request.Cnpj);
if (result.IsValid)
{
return Ok(new
{
isValid = true,
format = result.Format.ToString(),
normalizedCnpj = result.NormalizedCnpj
});
}
return BadRequest(new
{
isValid = false,
error = result.ErrorMessage
});
}
4. Normalization for Storage
public string NormalizeCnpjForStorage(string cnpj)
{
var validator = new CnpjValidator();
var result = validator.Validate(cnpj);
if (result.IsValid)
{
// Always store normalized (without formatting)
return result.NormalizedCnpj;
}
throw new ArgumentException($"Invalid CNPJ: {result.ErrorMessage}");
}
π§ͺ Testing
The library includes a complete test suite. To run:
dotnet test
Tests cover:
- Numeric CNPJ validation (traditional format)
- Alphanumeric CNPJ validation (new format)
- Check digit calculation
- Format identification
- Normalization
- Error handling (including rejection of all zeros and identical characters)
- Custom configurations
π¦ Library Structure
next-CNPJ/
βββ next-CNPJ/
βββ Core/
βββ Domain/
β βββ CnpjConfiguration.cs # Validation configuration
β βββ CnpjFormat.cs # Format enum
β βββ CnpjValidationResult.cs # Validation result
βββ Services/
β βββ CnpjValidator.cs # Validator implementation
β βββ ICnpjValidator.cs # Validator interface
β βββ CnpjFormatIdentifier.cs # Format identifier
β βββ ICnpjFormatIdentifier.cs # Format identifier interface
βββ Utilities/
βββ AsciiConverter.cs # ASCII conversion for calculation
βββ CnpjNormalizer.cs # CNPJ normalization
βββ DigitVerifierCalculator.cs # Check digit calculation
π References
- NTC 2025.001 - Technical specification for the new CNPJ format
- ENCAT - Excluded letters specification
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π€ Contributing
Contributions are welcome! Please open an issue or pull request.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.