How can I do math on the last x characters of a string and update the textbox on the fly?

I have an application where a user scans or enters a start barcode and the end barcode is automatically calculated based on the quantity value.

It works great when the start barcode is completely numeric, it does the math and includes leading zeros, so the end code is the correct length.

My problem is that a small percentage of barcodes are not fully numeric.

The barcode is 14 characters long. The last 5 characters will always be numeric, and the amount will rarely exceed a few hundred and will never be high enough to spill over into the 6th digit.

I'm not a javascript expert by any means and just getting what works for me now strains my skills - I hope the community here can help me :)

Here's the code I'm working with:

$(document).ready(function() {
  //leading zero fill function for barcodes
  function pad(num, size) {
    var s = "00000000000000" + num;
    return s.substr(s.length - size);
  }
  //Function to do BC math: starting number + quantity -1 (since it inclusive) = end. 
  function updateCode() {
    var quantity = $(this).closest('tr').find('.quantity').val();
    var barstart = $(this).closest('tr').find('.barstart').val();
    var end = pad(parseInt(barstart, 10) + parseInt(quantity - 1, 10), 14);
    $(this).parent().next().find('.barend').val(end);
  }
  $(document).on("change, keyup", ".barstart", updateCode);
});

      

Edit. Try pasting your HTML again:

<script src="//code.jquery.com/jquery-1.7.2.min.js"></script>
<table id="formtable">
<tr>
<td><input class="quantity" size="6" id="qty_1" name="qtyid_1" value="123" type="text"></td>
<td><input class="barstart" size="15" maxlength="14" id="barstartid_1" name="barstart_1" value="" placeholder="Barcode Start" type="text"></td>
<td><input class="barend" size="15" maxlength="14" id="barendid_1" name="barend_1" value="" placeholder="Barcode End" type="text"></td>
</tr>
</table>

      

Here's a jsfiddle: https://jsfiddle.net/g1p2xh6y/1/

Users can live without it, but it will save them some headaches (and help me make a good impression - this is a new gig) if I can get it to work, so I really appreciate any help the community can offer :)

Thank!

+3


source to share


1 answer


Thanks a lot @admcfajn - I didn't know the slice () function existed and that resolved it for me. I had to move the null's complement to the suffix and remove it from the final value, but since it pulls the prefix instead of doing the math, that's not a problem.

Here's the updated function:



function updateCode() {
        var quantity = $(this).closest('tr').find('.quantity').val();
        var barstart = $(this).closest('tr').find('.barstart').val();
        var barprefix = barstart.slice(0,barstart.length-5);
      var barendsuffix = pad(parseInt(barstart.slice(-5),10)+parseInt(quantity-1,10),5);
        $(this).parent().next().find('.barend').val(barprefix+barendsuffix);
}

      

0


source







All Articles