Skip to content

Instantly share code, notes, and snippets.

@zoghal
Created August 8, 2012 02:36
Show Gist options
  • Save zoghal/3291573 to your computer and use it in GitHub Desktop.
Save zoghal/3291573 to your computer and use it in GitHub Desktop.
Iranian MeliCode Validation
function checkMeliCode(code)
{
if(!/^\d{8,10}$/.test(code) || /^(0{8,10}|1{8,10}|2{8,10}|3{8,10}|4{8,10}|5{8,10}|6{8,10}|7{8,10}|8{8,10}|9{8,10})$/.test(code))
return false;
var L=code.length, _=0;
for(i=0;i<L-1;i++)
_+=code.charAt(i)*(L-i);
_%=11;
return (code.charAt(L-1)==((_<2)?_:11-_))
}
function checkMeliCode($code)
{
if(!preg_match('/^\d{8,10}$/', $code) OR preg_match('/^(0{8,10}|1{8,10}|2{8,10}|3{8,10}|4{8,10}|5{8,10}|6{8,10}|7{8,10}|8{8,10}|9{8,10})$/', $code))
return false;
$code = (string) $code;
$L = strlen($code);
$_=0;
for($i=0;$i<$L-1;$i )
$_ =$code[$i]*($L-$i);
$_%=11;
return ($code[$L-1]==(($_<2)?$_:11-$_));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment