Last active
January 2, 2018 06:04
-
-
Save jimishio/6bb08c29b1b3ed1bbe08ee89b5cb521a to your computer and use it in GitHub Desktop.
Alpha Numeric Incremental Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.