moses toh
moses toh

Reputation: 13192

How can I disable dot using keyup on vue.js 2?

I try like this :

<template> 
    ...
    <input type="number" class="form-control" v-model="quantity" min="1" v-on:keyup="disableDot">
    ...                           
</template>
<script>
    export default{
        ...
        methods:{
            disableDot: function(evt) {
                evt = (evt) ? evt : window.event
                let charCode = (evt.which) ? evt.which : evt.keyCode
                if (charCode === 190) {
                    evt.preventDefault()
                } 
                else {
                    return true;
                }
            }

        }
    }
</script>

If the code executed and I input dot(.), it still can

I want to disable dot. So user can not input dot

How can I do it?

Upvotes: 1

Views: 3138

Answers (1)

Ryosuke Suzuki
Ryosuke Suzuki

Reputation: 101

I don't know it can help, but this is my solution for your question.

new Vue({
  el: '#app',
  data: {
    quantity: ''
  },
  methods: {
    disableDot (e) {
      if (e.charCode === 46) {
        e.preventDefault()
      }
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.js"></script>

<div id="app">
  <input type="number" v-model="quantity" @keypress="disableDot"/>
</div>

Upvotes: 4

Related Questions