Code 128 Barcode Text Generator
Not working properly if the checksum value is > ascii 126
HTML
<p>Input (Paste) Barcode Value:</p>
<!-- Test with 500.77005.YELLO.XXXXX.0160828 -->
<input id="128" name="128" type="text" oninput="textTo128(this.value)" />
<p>Result</p>
<textarea id="barcodeTotal" cols="50" rows="2"></textarea>
JavaScript
function textTo128(str) {
/*
* Generate 128 Barcode text, suitable for copying and pasting.
*/
var len = str.length; //str.length - get length of string, used to generate the checksum.
var type128 = 104; // 128 Type B start
var typeClose = 106;
var total = 104;
var i; // Counter Variable
for(i=0;i<len;i++){
total += ((i+1) * (str.charCodeAt(i)-32)); //Multiply char position with decimal value of character, keep running total
}
var modVal = total % 103; // Use Modulus to find our checksum
var checksum = String.fromCharCode(modVal+32 > 126 ? modVal+32+18 : modVal+32);
if(modVal+32>126){alert(modVal+32);};
document.getElementById('barcodeTotal').innerHTML = String.fromCharCode(type128+100) + str + checksum + String.fromCharCode(typeClose+100);
}