New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

laravel-enso/cnp-validator

Romanian CNP validator for Laravel
11,976 2
Install
composer require laravel-enso/cnp-validator
Latest Version:5.0.4
PHP:^8.1
Laravel:10 – 13
Older releases:5 – 9
License:MIT
Last Updated:Aug 14, 2026
Links: GitHub  ·  Packagist
Maintainer: aocneanu

CNP Validator

License Stable Downloads PHP Issues Merge Requests

Description

CNP Validator provides a dedicated Laravel validation rule for Romanian personal numeric codes.

The package exposes a lightweight validation rule that checks whether a value is numeric, has the expected 13-digit length, contains a valid encoded birth date, and matches the official CNP checksum.

It works independently of the Enso ecosystem and can be used in any Laravel application that relies on Laravel's validator.

Installation

Install the package:

composer require laravel-enso/cnp-validator

No additional service provider registration is required.

Features

  • Registers a cnp validator rule through Laravel package auto-discovery.
  • Provides a Laravel validation rule through LaravelEnso\CnpValidator\Validators\Cnp.
  • Validates that the CNP contains only numeric digits.
  • Validates the required 13-character length.
  • Validates the encoded date portion of the CNP.
  • Validates the checksum using the official control hash table.
  • Exposes a reusable low-level validator helper through LaravelEnso\CnpValidator\Validators\Validator.

Usage

Use the rule in a form request or validator instance:

use Illuminate\Support\Facades\Validator;

$validator = Validator::make(
    ['cnp' => '1800219081826'],
    ['cnp' => ['nullable', 'cnp']],
);

Or use the rule object directly:

use Illuminate\Support\Facades\Validator;
use LaravelEnso\CnpValidator\Validators\Cnp;

$validator = Validator::make(
    ['cnp' => '1800219081826'],
    ['cnp' => ['nullable', new Cnp()]],
);

Example in a form request:

use Illuminate\Validation\Rule;
use LaravelEnso\CnpValidator\Validators\Cnp;

public function rules(): array
{
    return [
        'cnp' => [
            'nullable',
            'max:13',
            new Cnp(),
            Rule::unique('users', 'nin'),
        ],
    ];
}

::: warning Note The rule object reports failed validation with the Invalid message. The string rule reports failed validation with The :attribute must be a valid CNP..

If you want a localized or more specific validation message, map that message in your validation language files or customize it in the consuming validator layer. :::

API

Validation Rule

String rule:

  • cnp

The string rule is registered automatically by the package service provider and reports failed validation with The :attribute must be a valid CNP..

LaravelEnso\CnpValidator\Validators\Cnp

Public entry point:

  • validate(string $attribute, mixed $value, Closure $fail): void

The rule calls the internal validator and fails the field when the provided CNP is invalid.

Validator Helper

LaravelEnso\CnpValidator\Validators\Validator

Public methods:

  • __construct(?string $cnp = null)
  • fails(): bool
  • passes(): bool

Validation flow:

  • numeric-only check
  • 13-digit length check
  • encoded date validation
  • checksum validation

Depends On

Framework dependency:

Contributions

are welcome. Pull requests are great, but issues are good too.

Thank you to all the people who already contributed to Enso!

Related Packages

alcea/cnp

PHP Validation for Personal Identification Number (Validare PHP pentru Cod Numer...

83,373 11
laravel-enso/helpers

Helper classes dependency for Laravel Enso

106,576 8
laravel-enso/localisation

Language and translation management for Laravel Enso

65,106 13
laravel-enso/enums

Enums for Laravel Enso

57,948 0
laravel-enso/track-who

Created-by, updated-by, and deleted-by tracking traits for Laravel Enso

45,965 29

Version History

Version Released PHP Laravel License
5.0.4 ^8.1 ^10.0|^11.0|^12.0|^13.0 MIT
5.0.3 ^8.1 ^10.0|^11.0|^12.0|^13.0 MIT
5.0.2 ^8.1 ^10.0|^11.0|^12.0|^13.0 MIT
5.0.1 ^8.1 ^10.0|^11.0|^12.0|^13.0 MIT
5.0.0 ^8.1 ^10.0|^11.0|^12.0|^13.0 MIT