Skip to content

Instantly share code, notes, and snippets.

@jimishio
Last active January 2, 2018 06:04
Show Gist options
  • Save jimishio/6bb08c29b1b3ed1bbe08ee89b5cb521a to your computer and use it in GitHub Desktop.
Save jimishio/6bb08c29b1b3ed1bbe08ee89b5cb521a to your computer and use it in GitHub Desktop.
Alpha Numeric Incremental Javascript
function incAlphaNumeric(c) {
var u = c.toUpperCase();
var p = "";
var q = "";
var incrementalSign = 1;
var incrementalCounter = 1;
var initialLength = u.length;
var appender = "";
if(u.length > 0){
while(incrementalSign == 1){
p = u.substring(0, u.length - incrementalCounter);
q = u.substring(0, u.length - incrementalCounter + 1).slice(-1);
if(!isNaN(q)){
if(q==9){
q=0;
incrementalCounter++;
} else {
q++;
incrementalSign = 0;
}
} else {
if(q.toUpperCase() == 'Z'){
q ='A';
incrementalCounter++;
} else {
q = nextChar(q);
incrementalSign = 0;
}
}
u = p+q+appender;
if(incrementalCounter>1){
appender = u.substring(u.length - incrementalCounter + 1);
}
}
if(u.length > initialLength){
var _temp = u.substring(1);
var _temp1 = u.substring(0, 1);
u = _temp + _temp1;
}
return u.toUpperCase();
} else {
return "";
}
}
function nextChar(c) {
var i = (parseInt(c, 36) + 1 ) % 36;
return (!i * 10 + i).toString(36);
}
<!DOCTYPE html>
<html>
<head>
<title>increment</title>
<script type="text/javascript" src="alpha-numeric-increment.js"></script>
</head>
<body>
<input type="text" id="inputIncrement"/>
<button id="incrementBtn" onclick="myFunction()">Increment</button>
<span id="resultSpan"></span>
</body>
<script type="text/javascript">
var inputIncrement = document.getElementById("inputIncrement");
var incrementBtn = document.getElementById("incrementBtn");
var resultSpan = document.getElementById("resultSpan");
var myFunction = function(){
var resultString = incAlphaNumeric(inputIncrement.value.toString());
resultSpan.innerHTML = resultString;
}
</script>
</html>
@jimishio
Copy link
Author

jimishio commented Jan 2, 2018

There are some node modules available to increment alpha numeric strings but what they do is just increment the edge numbers like 9 to 10 so if I would give input 'f9' then output would come 'f10' but it should 'g0'.

So here it is. try it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment