Originally posted by bubblenut
How do you check if it's actually a valid number? Does this require checking up in some database?
to check it is of the form six digits, a hyphen, then 4 digits use
preg_match('/\d{6}-\d{4}$/',$string);
are there any other conditions which must be met?
Bubble
In most European countries there are certain rules for how to build a social security number. In Denmarks case, to validate a number use this function 😃 :
<?PHP
/**
Function validates Danish personal identification numbers.
Numbers may have format '070761-4285' or '0707614285'.
Rules for validation can be found at:
http://www.cpr.dk/Index/dokumenter.asp?o=11&n=0&d=397&s=4
http://www.cpr.dk/Index/dokumenter.asp?o=11&n=0&d=396&s=4
http://www.cpr.dk/Index/dokumenter.asp?o=11&n=0&d=393&s=4
@author xxx
@date 2004-02-13
@ string The personal identification number
@return bool true if ok
@return bool false if fails
*/
function checkDanishPIN($pin)
{
$pin = str_replace("-", "", $pin);
if(!is_numeric($pin)){
return false;
}
else if(strlen($pin) != 10){
return false;
}
$check_number = 4*$pin[0] + 3*$pin[1] + 2*$pin[2] + 7*$pin[3] + 6*$pin[4] + 5*$pin[5] + 4*$pin[6] + 3*$pin[7] + 2*$pin[8] + $pin[9];
if($check_number % 11 == 0){
return true;
}else{
return false;
}
}
$pin = '070761-4285';
if(validateDanishPIN($pin))
{echo 'PIN ok...';}
else
{echo 'PIN not ok...';}
?>