Validating MPAN Numbers in PHP

Validating MPAN Numbers in PHP

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.



13/04/2014 12:04:00 Shaun Freeman Filed Under: PHP PHP

Twitter Feed
Shaun Freeman @Zendmaster

Shaun Freeman @Zendmaster

I liked a @YouTube video https://t.co/lSFWmpHTX1 Patrick Stewart talks about meeting Sting on the set of DUNE (Funny to the EXTREME)

Shaun Freeman @Zendmaster

I added a video to a @YouTube playlist https://t.co/pmXSmod4ti Anonymous - This will Change Everything You Know... (2018-2019)

Shaun Freeman @Zendmaster

I added a video to a @YouTube playlist https://t.co/GkwTCvBfes Will Artificial Intelligence Take Over The World?

Shaun Freeman @Zendmaster

I liked a @YouTube video https://t.co/Y1ulafmsC6 Frank Abagnale: "Catch Me If You Can" | Talks at Google

Shaun Freeman @Zendmaster

I liked a @YouTube video https://t.co/NBdW2xFnqD ETS2: Special Transport DLC Trailer