info about link
: Javascript : PHP Index : MySQL :
elfproef
: Source : : Explanation : : Example : : Todo : : Feedback :

elfproef.php

Checks the validity of a dutch bank account number

Added: 2009-12-02 16:06:29

download

<?
/**
* This function can verify if a given number is a valid dutch bank-account number (not giro)
*
* @param String $bankAccountNr
*
* @return Boolean
*/
function elfProef($bankAccountNr)
{
   
$return   = False;
   
$length   = strlen($bankAccountNr);
   
$sum   = 0;
   
// we only continu if the length is exactly 9 characters
   
if (9 == $length)
   {
      for (
$i = 1; $i <= 9; ++$i)
      {
         
$sum += $i * substr($bankAccountNr, (9 - $i), 1);
      }

      if (
0 == $sum % 11)
      {
         
$return   = True;
      }
   }

   
// we return the results
   
return $return;
}


# The following class does the same as the function above, except that it can be used as a Zend_Validator;
# for those of you who are using the Zend Framework
# I would recommend using it if you can, since the it gives more feedback about what is wrong with the input
# than the function above


/**
* @category   Zend
* @package    Zend_Validate
* @copyright  Copyright (c) 2009 Martijn Korse (http://devshed.excudo.net)
* @license    http://framework.zend.com/license/new-bsd     New BSD License
*/
class Excudo_Validate_Elfproef extends Zend_Validate_Abstract
{
   
/**
    *  Validation failure message key for when the value has a length that is not exactly 9
    */
   
const INVALID_LENGTH      = 'lengthInvalid';

   
/**
    *  Validation failure message key for when the value is not a valid bank-account number
    */
   
const INVALID_ACCOUNT_NO      = 'bankAccountInvalid';

   
/**
    * Validation failure message template definitions
    *
    * @var array
    */
   
protected $_messageTemplates = array(
      
self::INVALID_LENGTH      => "'%value%' appears to have an invalid length",
      
self::INVALID_ACCOUNT_NO   => "'%value%' is not a valid (dutch) bank-account number"
   
);

   
/**
    * Constructor
    *
    * @return Excudo_Validate_Elfproef
    */
   
public function __construct()
   {
      ;
   }

   
/**
    * Defined by Zend_Validate_Interface
    *
    * Returns true if $bankAccountNr is a valid (dutch) bank-account number.
    * This validation will fail for postbank account-numbers!
    *
    * @param  string $bankAccountNr
    *
    * @return boolean
    */
   
public function isValid($bankAccountNr)
   {
      
$this->_setValue($bankAccountNr);

      
$return   = false;
      
$length   = strlen($bankAccountNr);
      
$sum   = 0;
      
// we only continue if the length is exactly 9 characters
      
if (9 == $length)
      {
         for (
$i = 1; $i <= 9; ++$i)
         {
            
$sum += $i * substr($bankAccountNr, (9 - $i), 1);
         }

         if (
0 == $sum % 11)
         {
            return
true;
         }
         else
         {
            
$this->_error(self::INVALID_ACCOUNT_NO);
         }
      }
      else
      {
         
$this->_error(self::INVALID_LENGTH);
      }
      return
false;
   }
}

download

=[Disclaimer]=     © 2005-2008 Excudo.net