Skip to content

Commit

Permalink
Refactor iranian national code validate
Browse files Browse the repository at this point in the history
  • Loading branch information
sadegh19b committed Mar 15, 2024
1 parent 68bd444 commit 31b5876
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/PersianValidators.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,25 +340,21 @@ public function validateIranianBankSheba($attribute, $value, $parameters)
*/
public function validateIranianNationalCode($attribute, $value, $parameters)
{
if (!preg_match('/^\d{8,10}$/', $value) || preg_match('/^[0]{10}|[1]{10}|[2]{10}|[3]{10}|[4]{10}|[5]{10}|[6]{10}|[7]{10}|[8]{10}|[9]{10}$/', $value)) {
if (!preg_match('/^\d{8,10}$/', $value) || preg_match('/^(.)\1{9}$/', $value)) {
return false;
}

$sub = 0;

$value = str_pad($value, 10, '0', STR_PAD_LEFT);

for ($i = 0; $i <= 8; $i++) {
$sub = $sub + ( $value[$i] * ( 10 - $i ) );
$sub = 0;
for ($i = 0; $i < 9; $i++) {
$sub += $value[$i] * (10 - $i);
}

if (( $sub % 11 ) < 2) {
$control = ( $sub % 11 );
} else {
$control = 11 - ( $sub % 11 );
}
$control = $sub % 11;
$controlDigit = $control < 2 ? $control : 11 - $control;

return (int) $value[9] === $control;
return (int) $value[9] === $controlDigit;
}

/**
Expand Down

0 comments on commit 31b5876

Please sign in to comment.