Nordic-ID-Validator
is a comprehensive toolkit for verifying Nordic personal identification numbers with accuracy and ease.
- Supports all major Nordic countries: Sweden, Norway, Denmark, and Finland.
- Provides both format and date validation.
- Lightweight with no external dependencies.
Using npm:
npm install nordic-id-validator
First, import the Validator class:
const Validator = require('nordic-id-validator');
Next, create an instance of the Validator class:
const validator = new Validator();
To validate a personal number, use the isValid
method by passing the personal number and the respective country code:
const isValid = validator.isValid('your_personal_number', 'SE'); // For Swedish numbers
Replace 'your_personal_number'
with the personal number you want to validate and 'SE'
with the respective country code. Valid country codes are:
SE
for SwedenNO
for NorwayDK
for DenmarkFI
for Finland
Example:
const isValidSwedish = validator.isValid('123456-7890', 'SE');
console.log(isValidSwedish); // true or false based on the validity
const isValidNorwegian = validator.isValid('12345678901', 'NO');
console.log(isValidNorwegian); // true or false based on the validity
You can also validate a personal number without specifying the country code. In this case, the country is specified by the the function. For example:
const isValidSwedish = validator.isValidSE('123456-7890');
console.log(isValidSwedish); // true or false based on the validity
The following country-specific methods are available:
isValidSE
for Swedish SSNisValidNO
for Norwegian SSNisValidDK
for Danish SSNisValidFI
for Finnish SSN
The isValid
method will throw an error if an invalid country code is provided or if the input is neither a string nor a number:
try {
const isValid = validator.isValid('your_personal_number', 'INVALID_COUNTRY_CODE');
} catch (error) {
console.error(error.message);
}
We welcome contributions! If you find a bug or have suggestions, please open an issue.
MIT
A project by Fiive.