Source for file FI.php
Documentation is available at FI.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | Copyright (c) 2006-2007 Jani Mikkonen |
// +----------------------------------------------------------------------+
// | This source file is subject to the New BSD license, That is bundled |
// | with this package in the file LICENSE, and is available through |
// | the world-wide-web at |
// | http://www.opensource.org/licenses/bsd-license.php |
// | If you did not receive a copy of the new BSD license and are unable |
// | to obtain it through the world-wide-web, please send a note to |
// | pajoye@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Jani Mikkonen <jani@mikkonen.info> |
// +----------------------------------------------------------------------+
* Specific validation methods for data used in Finland
* @author Jani Mikkonen <jani@mikkonen.info>
* @copyright 2006-2007 Jani Mikkonen
* @license http://www.opensource.org/licenses/bsd-license.php New BSD
* @version CVS: $Id: FI.php,v 1.3 2007/07/05 11:31:17 janisto Exp $
* @link http://pear.php.net/package/Validate_FI
* Validation class for Finland
* This class provides methods to validate:
* - Car License Plate Number
* - Motorbike License Plate Number
* - Personal Identity Number (HETU)
* - Unique Identification Number (SATU)
* - Business ID Number (Y-tunnus)
* - Party Identification Number (OVT-tunnus)
* - Value Added Tax Number (ALV-numero)
* - Bank Account Number (tilinumero)
* - Bank Reference Number (viitenumero)
* @author Jani Mikkonen <jani@mikkonen.info>
* @copyright 2006-2007 Jani Mikkonen
* @license http://www.opensource.org/licenses/bsd-license.php New BSD
* @version Release: 0.4.0
* @link http://pear.php.net/package/Validate_FI
// {{{ bool Validate_FI::postalCode( string $number [, bool $strong = false] )
* Validate Finnish postal code.
* Five digit postal code, maybe with a leading 'FI-'.
* Format: XXXXX or FI-XXXXX
* require_once 'Validate/FI.php';
* if ( Validate_FI::postalCode($postalCode) ) {
* @param string $number the postal code to be validated
* @param bool optional; strong checks
* (e.g. against a list of postal codes)
* @return bool true if postal code is valid, false otherwise
return (preg_match(" /^(FI-)?[0-9]{5}$/" , $number)) ? true : false;
// {{{ bool Validate_FI::phoneNumber( string $number )
* Validate Finnish telephone number.
* Simple check: number must be numeric when (, ), -, +, ., ' '
* chars are removed and 3-20 digit number.
* require_once 'Validate/FI.php';
* $phoneNumber = '+358 40 1234567';
* if ( Validate_FI::phoneNumber($phoneNumber) ) {
* @param string $number the telephone number to be validated
* @return bool true if telephone number is valid, false otherwise
$number = str_replace(Array ('(', ')', '-', '+', '.', ' '), '', $number);
return (preg_match(" /^[0-9]{3,20}$/" , $number)) ? true : false;
// {{{ bool Validate_FI::carLicensePlate( string $number )
* Validate Finnish car license plate number.
* Format: AAA-XXX, CD-XXXX or C-XXXXX. First or only number cannot be zero.
* AAA-XXX: AAA is 2-3 letters UPPERCASE A-Z + ÅÄÖ and XXX is 1-3 numbers.
* CD-XXXX: CD- and XXXX is 1-4 numbers (diplomat licence plate)
* C-XXXXX: C- and XXXXX is 1-5 numbers (other tax-free diplomat licence plate)
* require_once 'Validate/FI.php';
* $carLicensePlate = 'ABC-123';
* if ( Validate_FI::carLicensePlate($carLicensePlate) ) {
* @param string $number the license plate number to be validated
* @return bool true if license plate number is valid, false otherwise
* @link http://www.ake.fi/AKE/Rekisterointi/Suomen_rekisterikilvet/
// diplomat licence plate
if (preg_match(" /^CD-[1-9]{1}[0-9]{0,3}$/" , $number)) {
// other tax-free diplomat licence plate
if (preg_match(" /^C-[1-9]{1}[0-9]{0,4}$/" , $number)) {
if (preg_match(" /^[A-ZÅÄÖ]{2,3}-[1-9]{1}[0-9]{0,2}$/" , $number)) {
// {{{ bool Validate_FI::bikeLicensePlate( string $number )
* Validate Finnish motorbike license plate number.
* Format: AAAXXX. First or only number cannot be zero.
* Where AAA is 2-3 letters UPPERCASE A-Z + ÅÄÖ and XXX is 1-3 numbers.
* Letters and numbers are actually in separate lines.
* require_once 'Validate/FI.php';
* $bikeLicensePlate = 'ABC123';
* if ( Validate_FI::bikeLicensePlate($bikeLicensePlate) ) {
* @param string $number the license plate number to be validated
* @return bool true if license plate number is valid, false otherwise
* @link http://www.ake.fi/AKE/Rekisterointi/Suomen_rekisterikilvet/
return (preg_match(" /^[A-ZÅÄÖ]{2,3}(\n)?[1-9]{1}[0-9]{0,2}$/" , $number)) ? true : false;
// {{{ mixed Validate_FI::pin( string $number [, bool $info = false] )
* Validate Personal Identity Number (HETU).
* The Finnish PIN number (HETU) aka Social Security Number (SSN)
* is a 11 digit number with birthdate as ddmmyycxxxy where c is century,
* xxx is a three digit individual number and the last digit is a
* If xxx is odd it's a male PIN number and if even a female.
* Return gender (Male or Female) and date of birth (YYYY-MM-DD) in array if
* PIN is valid, is available by switching $info (2nd parameter) to true.
* Example: 010101-123N would be a male and born in 1st of January 1901.
* require_once 'Validate/FI.php';
* if ( Validate_FI::pin($pin) ) {
* if ( $userinfo = Validate_FI::pin($pin, true) ) {
* @param string $number PIN number to be validated
* @param bool optional; Return gender and date of birth on success
* @return mixed Returns true or false if $info = false or
* gender (Male or Female) and date of birth (YYYY-MM-DD)
* in array if PIN is valid, false otherwise
* @link http://koti.mbnet.fi/~thales/tarkmerk.htm#hetu1
function pin($number, $info = false )
static $control = array ("0","1","2","3","4","5","6","7","8","9",
"A","B","C","D","E","F","H","J","K","L",
"M","N","P","R","S","T","U","V","W","X","Y");
static $century = array ('+' => "18",
if(ereg ("^( [0-9 ]{2 })( [0-9 ]{2 })( [0-9 ]{2 })( [+-A ]{1 })( [0-9 ]{3 })( [0-9A-Z ]{1 })$" ,$pin,$regs)) {
// Validate date of birth. Must be a Gregorian date.
if(checkdate ($regs[2 ],$regs[1 ],$century[$regs[4 ]]. $regs[3 ])) {
if($control[intval ($regs[1 ]. $regs[2 ]. $regs[3 ]. $regs[5 ])%31 ] == $regs[6 ]) {
$gen = ($regs[5 ]%2==1 ) ? 'Male' : 'Female';
$dob = $century[$regs[4 ]]. $regs[3 ]. '-'. $regs[2 ]. '-'. $regs[1 ];
return array ($gen, $dob);
// {{{ bool Validate_FI::finuid( string $number )
* Validate Finnish Unique Identification Number (SATU).
* FINUID (SATU) is a 9 digit number. The last digit is a control number.
* require_once 'Validate/FI.php';
* if ( Validate_FI::finuid($finuid) ) {
* @param string $number FINUID number to be validated
* @return bool true if FINUID is valid, false otherwise
* @link http://koti.mbnet.fi/~thales/tarkmerk.htm#satu
static $control = array ("0","1","2","3","4","5","6","7","8","9",
"A","B","C","D","E","F","H","J","K","L",
"M","N","P","R","S","T","U","V","W","X","Y");
if(ereg ("^( [0-9 ]{8 })( [0-9A-Z ]{1 })$" ,$number,$regs)) {
if($control[intval ($regs[1 ])%31 ] == $regs[2 ]) {
// {{{ bool Validate_FI::businessId( string $number )
* Validate Finnish Business ID (Y-tunnus).
* The Finnish Business ID number (Y-tunnus) is a 9 digit number.
* The last digit is a control number (y).
* require_once 'Validate/FI.php';
* $businessId = '1572860-0';
* if ( Validate_FI::businessId($businessId) ) {
* @param string $number Business ID number to be validated
* @return bool true if Business ID is valid, false otherwise
* @link http://koti.mbnet.fi/~thales/tarkmerk.htm#y-tunnus2
if (preg_match(" /^[0-9]{6,7}-[0-9]{1}$/" , $number)) {
list ($num, $control) = split('[-]', $number);
// Add leading zeros if number is < 7
$num = str_pad($num, 7 , 0 , STR_PAD_LEFT );
$controlSum += (int) substr($num, 0 , 1 )*7;
$controlSum += (int) substr($num, 1 , 1 )*9;
$controlSum += (int) substr($num, 2 , 1 )*10;
$controlSum += (int) substr($num, 3 , 1 )*5;
$controlSum += (int) substr($num, 4 , 1 )*8;
$controlSum += (int) substr($num, 5 , 1 )*4;
$controlSum += (int) substr($num, 6 , 1 )*2;
$controlSum = $controlSum%11;
return ($controlSum == $control) ? true : false;
} else if($controlSum >= 2 && $controlSum <= 10 ) {
return ((11 - $controlSum) == $control) ? true : false;
// {{{ bool Validate_FI::partyId( int $number )
* Validate Finnish Party Identification number (OVT-tunnus).
* The Finnish Party Identification number (OVT-tunnus) is a 12-17
* digit number and it is generated from Business ID.
* Example: 0037AAAAAAAABBBBB, 0037 indicates Finland, AAAAAAAA is the
* Business ID and BBBBB is optional organization number.
* require_once 'Validate/FI.php';
* $partyId = '003715728600';
* if ( Validate_FI::partyId($partyId) ) {
* @param int $number Party Identification number to be validated
* @return bool true if number is valid, false otherwise
* @see Validate_FI::businessId()
* @link http://koti.mbnet.fi/~thales/tarkmerk.htm#alv-numero
$countryCode = substr($number, 0 , 4 );
$controlNum = substr($number, 11 , 1 );
$businessNum = substr($number, 4 , 7 );
$businessId = $businessNum. '-'. $controlNum;
// {{{ bool Validate_FI::vatNumber( string $number )
* Validate Finnish Value Added Tax number (ALV-numero).
* The Finnish VAT number (ALV-numero) is maximum
* of 14 digits and is generated from Business ID.
* require_once 'Validate/FI.php';
* $vatNumber = 'FI15728600';
* if ( Validate_FI::vatNumber($vatNumber) ) {
* @param string $number VAT number to be validated
* @return bool true if VAT number is valid, false otherwise
* @see Validate_FI::businessId()
* @link http://koti.mbnet.fi/~thales/tarkmerk.htm#alv-numero
$countryCode = substr($number, 0 , 2 );
$controlNum = substr($number, -1 , 1 );
$businessNum = substr($number, 2 , -1 );
$businessId = $businessNum. '-'. $controlNum;
// {{{ bool Validate_FI::bankAccount( string $number )
* Validate Finnish bank account number.
* This method checks the bank account number according to
* Finnish Bank Association.
* Format: XXXXXX-XXXXXXXX, 6 digits, - and 2-8 digits.
* require_once 'Validate/FI.php';
* $bankAccount = '159030-776';
* if ( Validate_FI::bankAccount($bankAccount) ) {
* @param string $number Finnish bank account number to be validated
* @return bool true if bank account is valid, false otherwise
* @link http://koti.mbnet.fi/~thales/tarkmerk.htm#pankkitili
* @link http://www.pankkiyhdistys.fi/sisalto/upload/pdf/tilinrorakenne.pdf
if (preg_match(" /^[0-9]{6}-[0-9]{2,8}$/" , $number)) {
// Bank groups are identified by the first digit
$bankType = substr($number, 0 , 1 );
// Group 1: First digit is 1, 2, 3, 6 or 8
$bankGroup1 = array ('1','2','3','6','8');
// Group 2: First digit is 4 or 5
$bankGroup2 = array ('4','5');
ereg("([0-9]{6})-([0-9]{2,8})", $number, $regs);
// Group 1: 999999-99999 -> 999999-00099999
$number = $regs[1 ] . str_pad($regs[2 ], 8 , 0 , STR_PAD_LEFT );
// Group 2: 999999-99999 -> 999999-90009999
} else if (in_array($bankType, $bankGroup2)) {
$number = $regs[1 ] . substr($regs[2 ], 0 , 1 ) .
// Now when we have a 14 digit bank account number, we can validate it.
// {{{ bool Validate_FI::refNum( string $number )
* Validate Finnish bank reference number.
* This method checks the bank reference number according to
* Finnish Bank Association.
* require_once 'Validate/FI.php';
* if ( Validate_FI::refNum($refNum) ) {
* @param string $number Finnish bank reference number to be validated
(spaces and dashes tolerated)
* @return bool true if reference number is valid, false otherwise
* @link http://koti.mbnet.fi/~thales/tarkmerk.htm#viitenumero
// Remove non-numeric characters from $refnum. Only 4-20 digit number.
// The last digit is a control number.
$controlNum = substr($refnum, -1 , 1 );
// Subtract control number from the $refnum.
$refNumCheck = ltrim($refNumCheck, 0 );
for($refLength = strlen($refNumCheck); $refLength > 0; $refLength-- ) {
$refSum += substr($refNumCheck, $refLength - 1 , 1 ) * $mul;
$refSum = substr(10 - ($refSum % 10 ), -1 );
if($refSum == $controlNum){
// {{{ bool Validate_FI::creditCard( string $number )
* Validate credit card number.
* This method checks the credit card number according to Luhn algorithm.
* This method doesn't guarantee that the card is legitimate.
* require_once 'Validate/FI.php';
* $creditCard = '5427 0073 1297 6425';
* if ( Validate_FI::creditCard($creditCard) ) {
* @param string $number credit card number to be validated
* (spaces and dashes tolerated)
* @return bool true if credit card number is valid, false otherwise
// Remove non-numeric characters from $number
// {{{ bool Validate_FI::_mod10( string $number )
* Validate number according to Luhn algorithm (mod 10).
* This method checks given number according Luhn algorithm.
* @param string $number to be validated
* @return bool true if number is valid, false otherwise
* @link http://en.wikipedia.org/wiki/Luhn_algorithm
// Double every second digit started at the right
for($i = strlen($number)-1; $i >=0; $i-- )
$doubledNumber .= ($odd) ? $number[$i]*2 : $number[$i];
// Add up each 'single' digit
for($i = 0; $i < strlen($doubledNumber); $i++ ) {
$sum += (int) $doubledNumber[$i];
// A valid number doesn't have a remainder after mod10 or equal to 0
return (($sum % 10 == 0 ) && ($sum != 0 )) ? true : false;
Documentation generated on Thu, 05 Jul 2007 08:00:04 -0400 by phpDocumentor 1.3.2. PEAR Logo Copyright © PHP Group 2004.
|