cleave.js demo

cleave.js demo

by shapeshifta

HTML

<script src="https://nosir.github.io/cleave.js/dist/cleave.min.js"></script>
<script src="https://nosir.github.io/cleave.js/dist/cleave-phone.i18n.js"></script>
<div class="container">
    <input class="input-credit-card" value="378282246310005" placeholder="Enter credit card number" />
    <button class="button-credit-card">
        Get raw credit card value
    </button>
</div>

CSS

* {
    box-sizing: border-box;
}
.container {
    width: 200px;
}
button {
    margin-bottom: 15px;
    width: 100%;
    height: 24px;
    outline: none;
    -webkit-appearance: none;
    border: 1px solid #e6e6e6;
    border-radius: 5px;
    background-color: #fff;
    font-size: 13px;
}
.input-credit-card {
    margin-bottom: 5px;
}
input {
    display: block;
    outline: none;
    -webkit-appearance: none;
    border: 1px solid #e6e6e6;
    border-radius: 5px;
    background-color: #fff;
    line-height: 30px;
    vertical-align: middle;
    height: 33px;
    width: 100%;
    font-size: 14px;
    padding: 0 8px;
    margin-bottom: 15px;
}

.input-numeral {
    text-align: right;
}

body {
    font-family: sans-serif;
}

a,
a:hover {
    color: #999;
}

a {
    font-size: 14px;
    margin-top: 20px;
}

JavaScript

// credit card
var cleaveCreditCard = new Cleave('.input-credit-card', {
    creditCard: true,
    creditCardStrictMode: true
});

document.querySelector('.button-credit-card').addEventListener('click', function() {
	alert(cleaveCreditCard.getRawValue());
});