After recently diving into Vue.js, I encountered a challenge. I needed an input field that only accepted numeric numbers; any other value entered by the user should be replaced with an empty string. To achieve this functionality, I decided to create a custom directive called 'numericOnly'.
Below is the code for my custom directive:
Vue.directive('numericOnly', {
bind (el, binding, vnode) {
regex = /^[0-9]*$/
if(!regex.test(el.value)){
el.value = el.value.slice(0, -1)
}
}
})
Here's how I implemented the custom directive in my template:
<input v-numericOnly name="mobileNumber" placeholder="Mobile Number" >
The issue I encountered was that the custom directive only ran once when the input field was first bound to the DOM.
I would greatly appreciate assistance in resolving this problem. Thank you in advance!