Skip to content Skip to sidebar Skip to footer

How To Create A Alphanumeric Serial Number In Javascript?

I am trying to create a alphanumeric serial number in Javascript, the serial number is governed by the following rules: 3-Digit Alphanumeric Series Allowed values 1-9 (Zero is exc

Solution 1:

This should do it:

var nextSerialNumber = function(serialNumber) {
    return (parseInt(serialNumber, 36) + 1).toString(36).replace(
      /i/g,'j').replace(/o/g, 'p').replace(/0/g, '1').toUpperCase();
}

nextSerialNumber("99Z") //=> "9A1"nextSerialNumber("11D") //=> "11E"

I'm not sure what you want to happen after ZZZ. It jumps to 1111, but that could be changed.

If you input an invalid serial number (e.g. 11I), it gives you the next valid number (e.g. 11J).

Solution 2:

var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
    var alphabetLen = alphabet.length;

    functionnextDigit(digit) {
        nextDigitPos = (alphabet.indexOf(digit)+1) % alphabetLen;
        return alphabet.charAt(nextDigitPos);
    }

    /**
     * Computes the next serial id.
     * @param id the id to compute the successor of,
     *        if null or empty String the first id
     *        "111" is returned.
     */functionnextSerial(id) {
        if(id==null || id.length==0) return"111";
        var digits = id.split("");
        digits[2] = nextDigit(digits[2]);
        if(digits[2] == "1") /* overflow */ {
            digits[1] = nextDigit(digits[1]);
            if(digits[1] == "1") /* overflow */ {
                 digits[0] = nextDigit(digits[0])
            }
        }
        return digits.join("");
    }

Solution 3:

This should do it:

functiongetNext(num) {
    var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
    var digits = num.toUpperCase().split(""),
        len = digits.length,
        increase = true;
    if (len != 3)
        thrownewError("Invalid serial number length in getNext: "+num);
    for (var i=len-1; increase && i>=0; i--) {
        var val = alphabet.indexOf(digits[i]);
        if (val == -1)
            thrownewError("Invalid serial number digit in getNext: "+num);
        val++;
        if (val < alphabet.length) {
            digits[i] = alphabet[val];
            increase = false;
        } else { // overflow
            digits[i] = alphabet[0];
        }
    }
    if (increase) // is still truethrownewError("Serial number overflow in getNext");
    num = digits.join("");
    return num;
}

Since you are working with a nearly alphanumeric alphabet, a parseInt/toString with radix 33 might have done it as well. Only you need to "jump" over the 0, I and O, that means replacing 0,A,B… by A,B,C…, replacing H,I,J… by J,K,L… and replacing M,N,O… by P,Q,R… (and everything back on deserialisation) - which might be OK if JS has a numeric char datatype, but I think it's easier to do it manually as above.

If you're curious:

String.prototype.padLeft = function(n, x) {
     return (newArray(n).join(x || "0")+this).slice(-n);
};
functiongetNext(num) {
    var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
    var back = {}, forth = {};
    for (var i=0; i<alphabet.length; i++) {
        var a = alphabet[i],
            b = i.toString(36);
        back[a] = b;
        forth[b] = a;
    }
    return (parseInt(num.replace(/./g, function(c) {
        return back[c]; // base33 from alphabet
    }), alphabet.length) + 1)
      .toString(alphabet.length)
      .padLeft(3)
      .replace(/./g, function(c) {
        return forth[c]; // base33 to alphabet
      });
}

Post a Comment for "How To Create A Alphanumeric Serial Number In Javascript?"