Last active
December 14, 2015 05:48
-
-
Save hugojay/5037525 to your computer and use it in GitHub Desktop.
JavsScript 國際條碼 EAN-13/UPC-A 格式檢核
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 checkBarcode(code){ | |
var result = false; | |
if(code.length >= 12 && code.length <= 13){ | |
if(code.length == 12){ | |
code = "0" + code; | |
} | |
var odd = 0, | |
even = 0; | |
for(var i in code){ | |
if(i%2 == 0){ | |
odd += code[i]*1; | |
}else{ | |
even += code[i]*1; | |
} | |
} | |
odd = odd - code[code.length-1]; | |
if((10-(even*3+odd)%10)%10 == code%10){ | |
result = true; | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment