Skip to content

Commit

Permalink
More IDnumber testing
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Sep 8, 2012
1 parent 7758707 commit bfdd1eb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/ValidationKit/TW/IDNumberValidator.php
Expand Up @@ -8,19 +8,16 @@ class IDNumberValidator extends Validator
public function calculateChecksum($str)
{
$c = (int) $str[0] * 1;
for($i = 1; $i < 11; $i++ ) {
for($i = 1; $i < 11; $i++ )
$c += $str[$i] * (10-$i);
}
$mod = $c % 10;
return 10 - $mod;
return 10 - ($c % 10);
}

public function validate($val) {
if( strlen($val) !== 10 )
return $this->invalid('length_error');
if( ! preg_match('#^[a-zA-Z]\d{9}$#i',$val) )
return $this->invalid('format_error');

$a = stripos('ABCDEFGHJKLMNPQRSTUVWXYZIO',$val[0]) + 10;
$c = $this->calculateChecksum($a . substr($val,1));
if( $c == substr($val,-1) )
Expand Down
2 changes: 2 additions & 0 deletions tests/ValidationKit/TW/IDNumberValidatorTest.php
Expand Up @@ -10,6 +10,8 @@ function test()
ok($v->validate('A178161208'));
ok($v->validate('A157354749'));
ok($v->validate('I155729562'));
not_ok($v->validate('BBBB'));
not_ok($v->validate('B123456789'));
}
}

0 comments on commit bfdd1eb

Please sign in to comment.