Skip to content

Instantly share code, notes, and snippets.

@hugojay
Last active December 14, 2015 05:48
Show Gist options
  • Save hugojay/5037525 to your computer and use it in GitHub Desktop.
Save hugojay/5037525 to your computer and use it in GitHub Desktop.
JavsScript 國際條碼 EAN-13/UPC-A 格式檢核
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