All Projects → olssonm → Identity Number

olssonm / Identity Number

Licence: mit
Validator for Swedish personal identity numbers (personnummer). For use "standalone" or with Laravel.

Projects that are alternatives of or similar to Identity Number

Credit Card
Credit Card Validation
Stars: ✭ 150 (+782.35%)
Mutual labels:  validation, laravel, laravel-5-package
Laravel Js Localization
🌐 Convert your Laravel messages and consume them in the front-end!
Stars: ✭ 451 (+2552.94%)
Mutual labels:  laravel, laravel-5-package
Telegram
✈️ Telegram Notifications Channel for Laravel
Stars: ✭ 450 (+2547.06%)
Mutual labels:  laravel, laravel-5-package
Sudo Su
Laravel package to easily login as other users during development.
Stars: ✭ 554 (+3158.82%)
Mutual labels:  laravel, laravel-5-package
Laravel Validation Rules
A set of useful Laravel validation rules
Stars: ✭ 374 (+2100%)
Mutual labels:  validation, laravel
Laravel Server Monitor
Server Monitoring Command for Laravel Applications
Stars: ✭ 424 (+2394.12%)
Mutual labels:  laravel, laravel-5-package
Column Sortable
Package for handling column sorting in Laravel 5/6/7/8
Stars: ✭ 496 (+2817.65%)
Mutual labels:  laravel, laravel-5-package
Jwt Auth Guard
JWT Auth Guard for Laravel and Lumen Frameworks.
Stars: ✭ 319 (+1776.47%)
Mutual labels:  laravel, laravel-5-package
Laravel Mysql Spatial
MySQL Spatial Data Extension integration with Laravel.
Stars: ✭ 578 (+3300%)
Mutual labels:  laravel, laravel-5-package
Laravel Video Chat
Laravel Video Chat using Socket.IO and WebRTC
Stars: ✭ 646 (+3700%)
Mutual labels:  laravel, laravel-5-package
Laravel Jsonapi
Basic setup framework for creating a Laravel JSON-API server
Stars: ✭ 16 (-5.88%)
Mutual labels:  validation, laravel
Laravel Translation
Translation management for your Laravel application.
Stars: ✭ 350 (+1958.82%)
Mutual labels:  laravel, laravel-5-package
Validator Docs
Validação de CPF, CNPJ, CNH, NIS, Título Eleitoral e Cartão Nacional de Saúde com Laravel.
Stars: ✭ 334 (+1864.71%)
Mutual labels:  validation, laravel
Lada Cache
A Redis based, fully automated and scalable database cache layer for Laravel
Stars: ✭ 424 (+2394.12%)
Mutual labels:  laravel, laravel-5-package
Generator
Laravel 5.3+ Scaffold Generator, Support both bootstrap and Semantic UI
Stars: ✭ 327 (+1823.53%)
Mutual labels:  laravel, laravel-5-package
Laravel Scout Mysql Driver
Laravel Scout MySQL Driver
Stars: ✭ 491 (+2788.24%)
Mutual labels:  laravel, laravel-5-package
Laravel Widgetize
A minimal package to help you make your laravel application cleaner and faster.
Stars: ✭ 791 (+4552.94%)
Mutual labels:  laravel, laravel-5-package
Mailgun
Mailgun package for Laravel
Stars: ✭ 297 (+1647.06%)
Mutual labels:  laravel, laravel-5-package
Laravel Log Enhancer
Make debugging easier by adding more data to your laravel logs (Laravel 5.6+)
Stars: ✭ 311 (+1729.41%)
Mutual labels:  laravel, laravel-5-package
Ip Location Zh
获取 IP 地址的真实地理位置
Stars: ✭ 556 (+3170.59%)
Mutual labels:  laravel, laravel-5-package

Swedish "personnummer" validator for Laravel

Latest Version on Packagist Software License Build Status Scrutinizer Score


⚠️ Abandoned This package has been abandoned in favor of olssonm/swedish-entity.

While the package will be usable for the forseeable future, it will not be updated. Please migrate to olssonm/swedish-entity when possible.


Validator for Swedish "personnummer" (a.k.a. personal identity number, social security number or simply "PIN").

This validator also handles Swedish organization numbers and the temporary personal identity number known as "Samordningsnummer" (a.k.a. coordination number).

For use either as a standalone package, or with Laravel.

The package does not only apply the Luhn-algorithm for the last four digits, but also checks that the date of birth is a valid date.

Of course you can throw pretty much any format you wish at the validator, ie. 10-digit variant (7712112775) or the 12-digit variant (197712112775) and with or without a hyphen (771211-2775, 19771211-2775).

Install

Via Composer

$ composer require olssonm/identity-number

Within Laravel

This package uses Package Auto-Discovery for loading the service provider. Once installed you should see the message

Discovered Package: olssonm/identity-number

Else, per standard Laravel-procedure, just register the package in your providers array:

'providers' => [
    Olssonm\IdentityNumber\IdentityNumberServiceProvider::class,
]

Usage

Standalone

The package is usable straight out of the box once installed with composer:

use Olssonm\IdentityNumber\Pin;

Personnummer ("personal identity number")

Pin::isValid('19771211-2775'); // Defaults to identity number
// true

Pin::isValid('19771211-2775', 'identity'); // Identity validation specified
// true

Organisationsnummer ("organization number")

Pin::isValid('556016-0681', 'organization')
// true

Samordningsnummer ("coordination number")

Pin::isValid('19671180-2850', 'coordination');
// true

The coordination-number validator handles the same way as the personal identity-validator but does not run a check/validation on the date of birth.

The IdentityNumberFormatter-class

The IdentityNumberFormatter-class allows you to format a PIN/identity for your custom needs. The class is also used internally for validation, but if you want to make sure you save the same value in the database as you pass for validation – you should also apply the formatting before validating.

use Olssonm\IdentityNumber\IdentityNumberFormatter;

// Format to a 10-character PIN without a seperator
$formatter = new IdentityNumberFormatter('19860210-7313', 10, false);

// Get the formatted output
$formatter->getFormatted(); // 8602107313

// You can also clean the number from all unwanted characters
(new IdentityNumberFormatter('a19860210 - 7313', 12, true))->clean()->getFormatted(); // 19860210-7313

Laravel validators

The package extends the Illuminate\Validator via a service provider, so all you have to do is use the identity_number-, coordination_number- and organization_number-rules, just as you would with any other rule.

// Personal identity numbers
public function store(Request $request) {
    $this->validate($request, [
        'number' => 'required|identity_number'
    ]);
}

// Coordination numbers
public function store(Request $request) {
    $this->validate($request, [
        'number' => 'required|coordination_number'
    ]);
}

// Organization numbers
public function store(Request $request) {
    $this->validate($request, [
        'number' => 'required|organization_number'
    ]);
}

And of course, you can roll your own error messages:

$validator = Validator::make($request->all(), [
    'number' => 'required|identity_number'
], [
    'number.identity_number' => "Hey! That's not a personnummer!"
]);

if($validator->fails()) {
    return $this->returnWithErrorAndInput($validator);
}

If you're using the validation throughout your application, you also might want to put the error message in your lang-files.

Testing

$ composer test

or

$ phpunit

License

The MIT License (MIT). Please see License File for more information.

© 2020 Marcus Olsson.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].