Created
August 8, 2012 02:36
-
-
Save zoghal/3291573 to your computer and use it in GitHub Desktop.
Iranian MeliCode Validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-_)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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