Answer the question
In order to leave comments, you need to log in
How to prevent "e" and "+-" characters from being entered in type="number"?
I include Alpine.js as in the dock:
<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.js" defer></script>
<input type="number" x-data="{}" @input="if( $event.data === 'e' ) { $el.value = $el.value.replace('\[e]\i', '') }">
Answer the question
In order to leave comments, you need to log in
Here is an option, but it does not provide for the paste situation via ctrl+v:
<input type="number" x-data="{}" @keydown="if( ['+','-','e'].includes( $event.key ) ) $event.preventDefault()">
the problem with cleanup is this:
what do you think is equal before being assigned to ?
$el.value = $el.value.replace('\[e]\i', '')
$el.value.replace('\[e]\i', '')
$el.value
If you check keypress or even keydown - you will not need to do any replacements and cleanups, but you can simply do preventDefault and "cancel" the button click itself.
But input still needs to be checked, because can paste via ctrl+v.
And in general, why don't you like 'E'?
123e3 is 123000 (three zeros at the end)
1e10 is 1000000000000 (10 zeros)
These are quite valid numbers and they are.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question