Skip to content

Instantly share code, notes, and snippets.

@Septdir
Last active February 11, 2021 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Septdir/db3d7b08fc1dc45758ee681731e87a46 to your computer and use it in GitHub Desktop.
Save Septdir/db3d7b08fc1dc45758ee681731e87a46 to your computer and use it in GitHub Desktop.
Numbers only
<input type="text" numbers-only >
<script>
document.addEventListener('DOMContentLoaded', function () {
document.querySelectorAll('input[numbers-only]').forEach(function (input) {
input.addEventListener('input', function () {
input.value = input.value.replace(/[^.\d]+/g, '').replace(/^([^.]*\.)|\./g, '$1');
});
});
});
</script>
document.addEventListener('DOMContentLoaded', function () {
document.querySelectorAll('input[numbers-only]').forEach(function (input) {
input.addEventListener('input', function () {
input.value = input.value.replace(/[^.\d]+/g, '').replace(/^([^.]*\.)|\./g, '$1');
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment