Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
make a directive that will adjust the number of characters:
const handlers = new Map();
function updateHandler(el, binding) {
el.removeEventListener('input', handlers.get(el));
const handler = () => {
const value = el.value.replace(/\D/g, '').slice(0, binding.value);
if (value !== el.value) {
el.value = value;
el.dispatchEvent(new Event('input'));
}
};
handler();
handlers.set(el, handler);
el.addEventListener('input', handler);
}
Vue.directive('maxlen', {
bind: updateHandler,
update: updateHandler,
unbind(el) {
el.removeEventListener('input', handlers.get(el));
handlers.delete(el);
},
});
<input type="number" v-maxlen="2">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question