PHP: check if 'rijksregisternummer' is valid. - developed.be

  • Published:September 18th, 2013
  • Category:php
  • 2 comments

I couldn’t find a decent PHP function to check the validation of the so called “rijksregisternummer” in Belgium (or “numéro de registre national” in French). I did however find a well written C# function by Tjeuten, so I decided to translate his C# function into PHP.

This is the result (html form checker incl):

<form method="get">
Rijksregisternummer: <input type="text" name="rijksregisternr" value="93051822361" /><br>
Gender:<br>
<input type="radio" name="gender" checked value="">unknown<br>
<input type="radio" name="gender" value="F">female<br>
<input type="radio" name="gender" value="M">male<br>
<input type="submit" />
</form>

<?php
 
if(isset($_GET['rijksregisternr']) && isset($_GET['gender'])){
    $check = isValidRijksregisterNumber($_GET['rijksregisternr'], $_GET['gender']);
    if($check === true){
        print("Valid!");
    }else{
        print("Invalid: " . isValidRijksregisterNumber($_GET['rijksregisternr'], $_GET['gender']));
    }
}
 
/**
 * Check if rijksregisternummer is valid
 * Based on C# function of http://www.tjeuten.be/tag/rijksregisternummer/ [Tjeuten]
 *
 * @param $rijksregisterNr
 * @param string $givenGender ("F" for female, "M" for male, empty string if unknown (default)
 * @return bool|string true if valid, errorstring if invalid
 */
function isValidRijksregisterNumber($rijksregisterNr, $givenGender = '')
{
    if (!is_null($rijksregisterNr))
    {
        $toReturn = "";
 
        $allowedCharactersPattern = "/^[0-9\ \-]+$/";
        $nonNumericPattern = "/\D/";
 
        if (preg_match($allowedCharactersPattern, $rijksregisterNr) != 1){
            return "Non allowed character";
        }else{
            //STRIP THE NUMBER FROM ALL NON NUMERIC CHARS
            $rijksregisterNr = preg_replace($nonNumericPattern, "", $rijksregisterNr);
 
            //LENGTH MUST BE 11 DIGITS
            if (strlen($rijksregisterNr) != 11) {
                return "Length != 11";
            } else {
                $birthDateOK = false;
                $counterOK = false;
                $genderOk = false;
                $controlOK = false;
                $born2kOrLater = false;
 
                //FIRST 6 DIGITS ARE BIRTHDATE IN FORMAT YYMMDD
                $birthDatePart = substr($rijksregisterNr, 0, 6);
 
                //NEXT 3 ARE COUNTER
                $counterPart = substr($rijksregisterNr, 6, 3);
 
                //LAST 2 ARE CONTROLNUMBER
                $controlPart = substr($rijksregisterNr, 9, 2);
 
                /* 1. CONTROL NUMBER CHECKING */
                /******************************/
 
                //CALCULATE CONTROLNUMER (= MOD 97 OF FIRST 9 DIGITS)
                $calculatedControl = 97 - (($birthDatePart . $counterPart) % 97);
 
                if ($calculatedControl != $controlPart){
                    /* IF THE CALCULATED CONTROL PART IS DIFFERENT THAN THE ONE IN THE INPUTSTRING
                     * ADD A "2" IN FRONT OF THE BIRTHDATEPART AND RECALCULATE. THIS WAS INTRODUCED TO
                     * ALLOW BIRTHDATES OF YEAR 2000 AND LATER
                    */
 
                    $calculatedControl = 97 - (("2" . $birthDatePart . $counterPart) % 97);
 
                    if ($calculatedControl != $controlPart){
                        /* THE CALCULATION STILL DOESN'T MATCH THE CONTROLNUMER, SO THIS IS AN INVALID
                         * REGISTRY NUMBER
                        */
                        $controlOK = false;
                    }else{
                        $born2kOrLater = true;
                        $controlOK = true;
                    }
                }
                else{
                    $controlOK = true;
                }
 
                /* 2. BIRTHDATE CHECKING */
                /*************************/
 
                $birthDate = $birthDatePart;
                //BUILD THE BIRTHDATE TO CHECK
                if ($born2kOrLater == true){
                    $birthDate = "20" . $birthDatePart;
                } else {
                    $birthDate = "19" . $birthDatePart;
                }
                //END BUILD
 
                $birthDateOK = checkdate(substr($birthDate, 4, 2), substr($birthDate, 6, 2), substr($birthDate, 0, 4));
 
                /* 3. COUNTER CHECKING */
                /***********************/
 
                /* COUNTERPART MUST BE BETWEEN 001 AND 997
                 * EVEN FOR FEMALE
                 * ODD FOR MALE
                */
 
                if ($counterPart < 1 || $counterPart > 997){
                    $counterOK = false;
                } else if ($counterPart % 2 == 0) { //EVEN
                    $counterOK = true;
                    $gender = "F"; //FEMALE
                } else {
                    $counterOK = true;
                    $gender = "M"; //MALE
                }
 
                if($givenGender != ""){
                    if($givenGender == $gender){
                        $genderOk = true;
                    }
                } else {
                    $genderOk = true;
                }
 
                /* 4. RETURN GENDER OR MESSAGE */
                /*******************************/
 
                $message = "";
 
                if (!$birthDateOK){
                    $message .= "Invalid birthdate;";
                }
                if (!$counterOK){
                    $message .= "Invalid counter;";
                }
                if (!$controlOK){
                    $message .= "Invalid control number;";
                }
                if (!$genderOk){
                    $message .= "Invalid gender;";
                }
 
                if ($message != "") //THERE ARE ERRORS
                {
                    return $message;
                } else {
                    return true;
                }
            }
        }
    }
    else
    {
        return "isnull";
    }
}


Rss Comments

2 comments

  1. Hi! Great blog template. What is its name?

    #1 Brady
  2. A tweaked version of Minibits.

    #2 Robin

Leave a comment