Last active
January 8, 2019 05:07
-
-
Save dmjcomdem/3a887b5a972c0658e23ba7f0fcd88936 to your computer and use it in GitHub Desktop.
Cardcode trim input
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
<form name="myform"> | |
<input type="text" name="cardcode" pattern="^([0-9]{4}\s){3}[0-9]{4}$" placeholder="×××× ×××× ×××× ××××" required> | |
</form> |
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
let cardInput = myform.cardcode; | |
cardInput.addEventListener('input', formatCardCode, false); | |
function formatCardCode() { | |
let cardCode = this.value.replace(/[\D]/g, '').substring(0,16); | |
cardCode = cardCode != '' ? cardCode.match(/.{1,4}/g).join(' ') : ''; | |
this.value = cardCode; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment