Some time ago I come across need to validate electric MPAN (Meter Point Administration Number) numbers for a client in which users input the MPAN in a form and so we needed a way to make sure that nonsense numbers were not entered.

So I tried to find a ready made solution which after a long google search revealed nothing for a solution to validate these numbers in php but I did find a useful resource on Wikipedia http://en.wikipedia.org/wiki/Meter_Point_Administration_Number, but no php function was there so I set to converting the JavaScript function found there into php.

The test to verify a MPAN number is the modulus 11 test and is done on the last 13 digits, the last number being the check digit of the MPAN and goes thus:

  • Multiply the first digit by 3.
  • Multiply the second digit by the next prime number (5).
  • Repeat this for each digit (missing 11 out on the list of prime numbers for the purposes of this algorithm.).
  • Add up all these numbers.
  • The check digit is the sum modulo 11 modulo 10.

So the function in php to do this is:

function checkMPAN($mpan)
{
    $primes = array(3, 5, 7, 13, 17, 19, 23, 29, 31, 37, 41, 43);
    $sum = 0;
    $mpan = str_split($mpan);

    if ((count($mpan) - 1) == count($primes)) {
        for ($i = 0; $i < count($primes); $i++) {
            $sum += $mpan[$i] * $primes[$i];
        }
        return (($sum % 11 % 10) == end($mpan)) ? true : false;
    } else {
        return false;
    }
}

In a future post I will show how to make a Zend Framework implementation of this check using Zend_Form and Zend_Validate.

Anyway I hope this helps someone.