Last active
December 22, 2015 17:29
-
-
Save abhidilliwal/6506393 to your computer and use it in GitHub Desktop.
Credit card inout boxhttp://jsbin.com/eqIbEp/2
This file contains hidden or 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
#inp{ | |
border: 1px solid gray; | |
font-size: 24px; | |
padding: 5px; | |
outline: none; | |
width: 50%; | |
min-width: 300px; | |
} | |
#inp:hover,#inp:active{ | |
outline: none; | |
} |
This file contains hidden or 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> | |
<meta name="description" content="credit card formatter" /> | |
<meta charset=utf-8 /> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<input type=text id=inp maxlength="19" /> | |
</body> | |
</html> |
This file contains hidden or 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 (document, console){ | |
"use strict"; | |
var inp = document.getElementById("inp"); | |
function formatCC(txt){ | |
var ntxt = ""; | |
for (var i = 0; i< txt.length; i++) { | |
if (i !== 0 && i%4 === 0) { | |
ntxt += " "; | |
} | |
ntxt += txt[i]; | |
} | |
return ntxt; | |
} | |
inp.addEventListener("keyup", function(){ | |
var obj = this; | |
var noSpaceVal = obj.value.split(" ").join(""); | |
obj.value = formatCC(noSpaceVal); | |
}); | |
})(window.document, console); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment