Your Ad Here

Posted By

Pjotor on 08/02/10


Tagged

personnummer checksum EAN


Versions (?)

Pnr (swe) and EAN checksum prototype


 / Published in: JavaScript
 

URL: http://pjotor.com

EAN and Personnummer checksum/digit calculator.\r\n\r\nUsage: \r\n\"123456789\".ean() ==> \"1234567895\"\r\n\"123456789\".pnr() ==> \"1234567897\"\r\n\r\nNote: This does not validate checksum/digit, only calculates it.

  1. String.prototype.ean = function(){var n=0;var d=(this+'0').split('').reverse();for(var i in d){n+=(i%2)?d[i]*3:+d[i]}return this+(10-(n%10))%10}
  2.  
  3.  
  4. String.prototype.pnr = function(){var n=[];var d=(this).split(''); for(var i in d){n[i]=((+i+1)%2)?d[i]*2:+d[i]} d=n.join('').split(''); n=0; for(var i in d){n+=+d[i]} n=(10-(n%10)); return this + String(n).substr(String(n).length-1) }

Report this snippet  

You need to login to post a comment.