Vuetify Add Comma to Input Field Automatically

Vuetify is a popular Vue.js framework that allows developers to create modern and responsive user interfaces. In this post, we will discuss how to automatically add commas to input fields and format numbers as (USA) currency and (INR) currency in Vuetify text fields

Profile Photo

Abhishek Yadav (SD) SDE of FAI

Feedback

written
Profile Photo

Anil Kumar (Expert) Coding Expert of FAI

Feeback

reviewed
Profile Photo
Nov 06, 2023 02:11 AM Last Updated
updated

Adding Commas to Input Fields and Formatting Numbers as (USA) Currency

In the first example, we have a Vuetify text field for entering an amount. We want to add commas to the number as the user types and format it as (USA) currency. Here's the code snippet:

Vuetify Format Numbers as (USA) currency in Text Field

Copied to Clipboard
Run

We have also included a @input event listener that triggers the formatNumber method. Here's the JavaScript code for formatting the number

The formatNumber method replaces non-numeric characters with an empty string, ensuring that only numbers are considered. Then, it uses the Intl.NumberFormat to format the number as (USA) currency with commas.

Pasted Image

Adding Commas to Input Fields and Formatting Numbers as (INR) Currency

In the second example, we have another Vuetify text field for entering an amount, but this time we want to format the number as (INR) currency. Here's the code snippet:

Vuetify Format Numbers as (INR) Currency in Text Field

Copied to Clipboard
Run

Just like in the previous example, we have included a @input event listener that triggers the formatNumber method. Here's the JavaScript code for formatting the number as (INR) currency:

Similarly, the formatNumber method removes non-numeric characters and then uses Intl.NumberFormat to format the number as (INR) currency with commas

Pasted Image

Conclusion:

In this post, we have shown how to automatically add commas to input fields and format numbers as (USA) currency and (INR) currency in Vuetify text fields. By using the @input event listener and JavaScript methods, you can create a user-friendly and visually appealing input experience for your users when dealing with currency values in your web application.

 

Ad