Posted By

ping_ch on 03/30/09


Tagged

validation Banking iban accountnumber


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

ping_ch
Scooter
NyX


Validate IBAN


 / Published in: PHP
 

URL: validate_iban

This PHP function checks if a given string is a valid International Banking Acount Number (IBAN).

  1. <
  2. function _isValidIBAN($data, $reformat = true, $countryCode=null, $printErrors=false){
  3.  
  4.  
  5. $str = $data['account_number'];
  6.  
  7. if(!empty($str)){
  8. if($reformat){
  9. $str = preg_replace('/[\W_]+/', '', strtoupper($str) );
  10.  
  11. }
  12. $errors = array();
  13. if( 4 > strlen($str) || strlen($str) > 34 ){
  14. $errors[] = 'Invalid string length';
  15. }
  16.  
  17. if(is_numeric($str[0]) || is_numeric($str[1])){
  18. $errors[] = 'Invalid chars at 0, 1';
  19. }
  20. if(!is_numeric($str[2]) || !is_numeric($str[3])){
  21. $errors[] = 'Invalid chars at 3, 4';
  22. }
  23.  
  24. if(!empty($countryCode)){
  25.  
  26. if(!in_array(strtolower(substr($str, 0, 2)), array_map('strtolower', $countryCode)) ){
  27. $errors[] = 'Invalid Country code. Only accepting '. implode(', ', $countryCode);
  28. }
  29. }
  30.  
  31. $ibanReplaceChars = range('A','Z');
  32.  
  33. foreach (range(10,35) as $tempvalue) {
  34. $ibanReplaceValues[]=strval($tempvalue);
  35. }
  36.  
  37.  
  38. $tmpIBAN = substr($str, 4).substr($str, 0, 4);
  39.  
  40. $tmpIBAN = str_replace($ibanReplaceChars, $ibanReplaceValues, $tmpIBAN);
  41.  
  42. $tmpValue = intval(substr($tmpIBAN, 0, 1));
  43.  
  44. for ($i = 1; $i < strlen($tmpIBAN); $i++) {
  45. $tmpValue *= 10;
  46. $tmpValue += intval(substr($tmpIBAN,$i,1));
  47. $tmpValue %= 97;
  48. }
  49.  
  50. if ($tmpValue != 1) {
  51. $errors[] = 'Invalid checksum';
  52. }
  53.  
  54. if($printErrors){
  55. echo implode("\n", $errors);
  56. }
  57.  
  58. if(empty($errors)){
  59. return true;
  60. }
  61. }
  62.  
  63. return false;
  64. }

Report this snippet  

You need to login to post a comment.