Filter input text to enter only number and not even decimal Filter input text to enter only number and not even decimal vue.js vue.js

Filter input text to enter only number and not even decimal


From the Beufy doc, I get that <b-input> is essentially an extension of native <input> field so it accepts attribute that the native input would accept.

As of now, it is not possible to completely filter out specific characters using pure HTML attributes like pattern="\d+".

What you can do is to use a "keydown" event listener to filter out these characters using native event.preventDefault() by respective keys. Of course, we could use the native <input type="number"> to help in general.

const app = new Vue({  el: '#app',  methods: {    filterKey(e){      const key = e.key;      // If is '.' key, stop it      if (key === '.')        return e.preventDefault();            // OPTIONAL      // If is 'e' key, stop it      if (key === 'e')        return e.preventDefault();    },        // This can also prevent copy + paste invalid character    filterInput(e){      e.target.value = e.target.value.replace(/[^0-9]+/g, '');    }  }});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script><div id="app">  <input    type="number"    step="1"    min="0"        @keydown="filterKey"    // OR     @input="filterInput"  ></div>


I have just started using vue js so i don't have much knowledge but i think you can add an event listener and use reg ex in your function

<input type="number" @input="checknum">export default{    methods: {        checknum: function (event) {            this.value = this.value.replace(/[^0-9]/g, '');        }    }}


You can call a function on keyup event and check all the non-numeric characters and remove from the value.For Example:

// In template add this line  <input type="text" v-model="inputNumber" @keyup="onlyNumeric" />// Define inputNumber in data.// In Methods add onlyNumeric function  onlyNumeric() {     this.inputNumber = this.inputNumber.replace(/[^0-9]/g, '');}