0

I'm trying to work out how to increase a weird record numbering system I think I'm on the right track but is there a better way?

The record numbering works like this member number - year - 4 digit number e.g. 1076-14-0081

I need to increase the 0081 part of the number to be the next number.

When I load the input form up I call in the last number to a text field animalId and focus on the text field. Once they exit out of the field the code below is run and the field is updated.

$(function() {
    $("#animalId").focusout(function() {
        curNum = $("#animalId").val();
        animalIDArray = curNum.split('-');
        memberNumber = animalIDArray[0];
        var year = Number(animalIDArray[1]);
        animalNumber = Number(animalIDArray[2]);
        animalNumber = animalNumber +1;
        animalNumber = String("0000" + animalNumber).slice(-4);
        result = [memberNumber, year, animalNumber].join("-");
        $("#animalId").val(result);
        }); 
});

I was wondering have I done it correctly or is there a better way I can do this automatically?

Rod
  • 43
  • 1
  • 4
  • Looks broadly fine, although I've never seen 'Number()' before? It's usually parseInt() I think. You might need to make it more robust against bad inputs, though, e.g. if they enter the wrong number of dashes: it might be better just to take the last four digits, or the last block of digits up to the dash rather than breaking up the whole string. You might also want to store a flag 'have I incremented the number' so that you don't accidentally do it twice for the same record if they e.g. tab around the form. – Rup Nov 29 '14 at 10:14
  • 1
    And there's no real need to convert `year` to a number. In fact, thinking about it, that would have caused a problem in (say) 2008, you'd've ended up with `1076-8-0082`. – T.J. Crowder Nov 29 '14 at 10:15
  • 5
    This question appears to be off-topic because it is about code review, it might find a home on http://codereview.stackexchange.com. – T.J. Crowder Nov 29 '14 at 10:15

2 Answers2

1

You can use a RegExp, you code will be shorter but maybe not faster (you will have to test):

var result = $('#animalId').val().replace(/[0-9]{4}$/, function (number) {
    return ("000" + (parseInt(number) + 1)).slice(-4);
});
KyleK
  • 4,643
  • 17
  • 33
0

This is a case where you mix data and output. It is often like that with websites though.

To be cleaner way, although not in any way faster, would be to have an object such as:

my_id.memberNumber = 123;
my_id.year = 2014;
my_id.animalNumber = 189;

Then you can, obviously do a ++ on the animalNumber

++my_id.animalNumber;

Finally, you want a function that generates the output:

output = [my_id.memberNumber, my_id.year, my_id.animalNumber].join('-');

This is the clean way of doing things. How you get the my_id initialized may be difficult if you do not have full control over what the server generates... The following is an example pretty much following your scheme (i.e. getting the value from an input widget.)

So in a JavaScript object oriented implementation you do something like this:

my_id = function(val)
{
    var v = val.split('-');
    my_id.memberNumber = v[0];
    my_id.year = v[1];
    my_id.animalNumber = v[2];
};

my_id.prototype.nextAnimal = function()
{
    ++this.animalNumber;
};

my_id.prototype.toString = function()
{
    return [my_id.memberNumber, my_id.year, my_id.animalNumber].join('-');
};

// make use of object:
var id = new my_id($("#animalId").val());
id.nextAnimal();
$("#animalId").val(id.toString());

It may look a lot more complicated for nothing, but just wait to add 20 more functions and such... and you'll be happy to have a class instead of plain code.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156