Can Vue.js add commas while user typing numbers?

20,058

Solution 1

You don't need jQuery at all for this. You watch your variable, and in the watch function, compute the reformatted version, then set it back to your variable using nextTick (so it's not mutating before the watch is complete).

new Vue({
  el: '#vue',
  data: {
    price: 0
  },
  watch: {
    price: function(newValue) {
      const result = newValue.replace(/\D/g, "")
        .replace(/\B(?=(\d{3})+(?!\d))/g, ",");
      Vue.nextTick(() => this.price = result);
    }
  }
});
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/1.0.28/vue.min.js"></script>
<div id="vue">
  <input type="text" v-model="price" />{{price}}
</div>

Solution 2

To people who are looking for doing mask for multiple fields then it's kinda pain to use watch, so may be we can use v-money (docs included). And check the demo here.

Solution 3

If you are using Vuetify, a new light-weight library called 'vuetify-money' has been published. Super easy to use for money value inputs, it is a text field that will add commas as you type. Here's a demo.

All properties you use on a v-text-field can also be used, so it is easily customizable.

Step 1

npm install vuetify-money --save

Step 2

Create a src/plugins/vuetify-money.js file with the following content:

import Vue from "vue";
import VuetifyMoney from "vuetify-money";
Vue.use(VuetifyMoney);
export default VuetifyMoney;

Step 3

Add file to src/main.js :

import "./plugins/vuetify-money.js";

(main.js is the file where you usually put this)

new Vue({render: h => h(App)
}).$mount('#app');

Step 4 Use it in your code !

<template>
  <div>
    <vuetify-money
      v-model="value"
      v-bind:options="options"
    />
    Parent v-model: {{ value }}
  </div>
</template>
<script>
export default {
  data: () => ({
    value: "1234567.89",
    options: {
      locale: "ja-JP",
      prefix: "$",
      suffix: "",
      length: 10,
      precision: 2
    }
  })
};
</script>

You now have a text field that will add commas as you type while keeping the v-model values perfectly fine. It also prevents any non-number inputs so you hardly need front-end validation checks excluding customized cases.

Share:
20,058
Morteza Negahi
Author by

Morteza Negahi

I Love The Web Programming . And Practicing To This Way.

Updated on August 05, 2022

Comments

  • Morteza Negahi
    Morteza Negahi almost 2 years

    I see this topic, But this is Jquery, how can I change it to Vue.js ? Is v-on supported in Vue.js? Where is my mistake?

    <div id="vue">
        <input v-model="amountModel" v-on:keyup="AddCammas()" value="{{price}}">
    </div>
    
    <script>
       el: #vue,
       methods:{
          AddCammas : funtion(){
              if(event.which >= 37 && event.which <= 40) return;
    
              $(this).val(function(index, value) {
                 this.message = this.amountModel
                    .replace(/\D/g, "")
                    .replace(/\B(?=(\d{3})+(?!\d))/g, ",");
               });
          } 
       }   
    </script>