Number only
This is to not accept any character besides from number and/or decimal.
by jonjieviduya
HTML
<p>Highlight me and drag to the input to test.</p>
<input type="text" class="integer" placeholder="Enter any character here">
<input type="text" class="decimal" placeholder="This input will allow decimal">
CSS
input {
display: block;
padding: 5px 8px;
margin: 10px;
border: solid 1px #14e6f0;;
}
JavaScript
document.querySelector('.integer').addEventListener('input', function() {
this.value = this.value.replace(/[^0-9]/g, '').replace(/(\..*)\./g, '$1');
});
document.querySelector('.decimal').addEventListener('input', function() {
this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');
});