I am attempting to incorporate the onkeypress
event within a Vue component. My goal is to allow only numbers on keypress while disallowing all other key codes. Here is what I have tried:
Implementing the onkeypress event works perfectly!
<input type="number" onkeypress="return (event.charCode === 0 || /\d/.test(String.fromCharCode(event.charCode)));" />
However, when I attempt to convert this to be used in Vue, it fails to work!!! :(
<input type="number" @keypress="restrictChars($event)" />
<script>
export default {
name: 'quantity-selector',
methods: {
restrictChars: function($event) {
return ($event.charCode === 0 || /\d/.test(String.fromCharCode($event.charCode)));
}
}
What could I possibly be overlooking?? I am struggling to comprehend where the issue lies. Any assistance would be greatly appreciated!