Guidelines for Decimal Entry in German Language

I need help finding a vue3 directive for formatting German decimal values. The issue I'm experiencing is that I want to display the decimal value in German format, but store it with a "." as the decimal separator.

My current test implementation is not working as expected. The on blur event seems to be replacing the entire model instead of updating just the decimal value.

Custom Directive

export default {
    beforeMount(el, binding) {
        el.pattern = '^d+(,d{1,2})?$';
        el.addEventListener('blur', (event) => {
            let value = event.target.value.replace(',', '.');
            const numericValue = parseFloat(value);
            if (!isNaN(numericValue)) {
                binding.instance.$emit('update:modelValue', numericValue);
                el.value = numericValue.toLocaleString('de-DE', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
            } else {
                el.value = '';
            }
        });
    },
};

Usage Example

 <input v-model="myObj.multiplicator" v-decimal required />

Any suggestions on how to fix this issue?

Answer №1

Instead of manually implementing currency formatting, I recommend utilizing the Browser Standard Intl.NumberFormat API. You can achieve this by using the following code:

new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number)

You can encapsulate this in a method and pass your desired currency to ensure proper formatting on your user interface.


For a more visual explanation, you can refer to this YouTube video.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Please ensure each child within a list in a React component function has a distinct "key" prop assigned to it. Verify this in the render method

So here's the situation. Everything was running smoothly in my project until I added this specific component. Now, it's throwing out a warning message: Warning: Each child in a list should have a unique "key" prop. Check the render method I&apo ...

React's setInterval acting unpredictably

Creating a stopwatch functionality in React was my recent project. To achieve updating the number every second, I implemented setInterval as follows: import React, {useEffect, useState, useRef} from 'react'; const MAX_TIMER = 5; export defa ...

Issue with NodeJS Express's reverse proxy due to an invalid TLS certificate alternative name

I have configured a reverse proxy on my endpoint as shown below: var express = require('express'); var app = express(); var httpProxy = require('http-proxy'); var apiProxy = httpProxy.createProxyServer(); var serverOne = 'https://i ...

javascriptconst eventEmitter = require('events').EventEmitter;

Have you ever wondered why certain code snippets work while others don't? Take for example: var EventEmitter = require('events').EventEmitter; var channel = new EventEmitter(); this code works perfectly fine, but when we try something like ...

Transform this JavaScript into Vue 3 code

Hey there! I'm currently working on implementing dark mode into my project by following a tutorial. However, the tutorial is based on JavaScript and not Vue, so I'm having some trouble converting this particular section of code to work with Vue 3 ...

Guide on Hosting Vue.js Project on a Live Server

Could someone confirm if my setup for a vue.js 2.6 upload is correct? As far as I know, the only file that needs to be modified is the index.html file located in the techjobs folder. I have made adjustments to this file to match the directory structure. ...

See CoffeeScript from the perspective of Compiled JavaScript

https://i.sstatic.net/JRNjG.png I recently started learning Coffeescript and decided to install the 'BetterCoffeeScript' package on Sublime. I am able to see the syntax highlighting, but I'm struggling to view my compiled javascript code. W ...

The onclick event is malfunctioning in Chrome when dealing with data driven by SQL

<select id='city' name='city' > <?php $dbcon = mysql_connect($host, $username, $password); mysql_select_db($db_name,$dbcon) or die( "Unable to select database"); $city_query = "SELECT city,county FROM citycatalog order by city ...

Sending a list of data to a child component in Vue

After reviewing various answers on here, such as this one, it seems like passing imported JSON data from the parent to the child component should work. The import and looping through data functions perfectly if I do it all in the parent with a v-for: < ...

Using Vanilla JavaScript to Disable a Specific Key Combination on a Web Page

On a completely random English-Wikipedia editing page, there exists a way to add content (for example, "test") and save it using the existing key combination of Alt+Shift+S. My goal is to specifically prevent this action without removing the save button b ...

integrating html within an input element

I need help adding break tags at the end of each line of code in my input box to prevent it from appearing as one continuous line. I tried encoding the break tag as HTML, but it's not displaying correctly when I type it in. Here is a snippet of what ...

Can a form be submitted using an Ajax request triggered by a div element instead of an input type submit?

I used to submit the form by triggering $(".btn-update.").click() before, but now that I'm using $(".btn-update").ajaxForm(), the form is not getting submitted. Here is the HTML code: <form id="company-profile-edit-form" name="company-profile-edi ...

A comprehensive guide on utilizing child-process-promise

const executeCommand = require('child-process-promise').spawn; executeCommand('some_command_producing_output') .then(function (result) { ... }) .catch(function (err) { ... }); I am looking to enhance th ...

JavaScript function that loads different configuration files based on the URL

My mobile website currently fetches a config.json file using the provided JavaScript code: $.ajax({ type:'GET', url: '/config.json', contentType: 'plain/text; charset=UTF-8', dataType: 'js ...

The Angular $rootScope.user.userMessage throws an error stating that it is not an object

I am encountering an issue: Error: The object '$rootScope.user.userMessage' is not defined. (evaluating 'typeof $rootScope.user.userMessage') This error arises from the following code snippet: if (typeof($rootScope.user.userMessage ...

Clear the applied style from a specific DIV element

Currently, I am working on iPhone development and have set up a webview with a DIV element. Within this DIV, I have added 'touchstart', 'touchend', and 'touchmove' events. However, I have noticed that after adding these event ...

Expanding jQuery Accordion on clicking a link within the panel

I have implemented an accordion feature on my webpage and I am looking to include hyperlinked text within each expanded panel. By clicking on the link 'Reduce text', I want to be able to collapse the accordion. How can I modify the existing code ...

Clicking on Rows in DataTables: Enhancing

I have been utilizing the jquery datatables plugin, and have encountered an issue with the row click functionality. Strangely, it only seems to work on the first page of the table. When I navigate to any subsequent pages, the row click fails to respond whe ...

Is it necessary to utilize multiple v-models in Vue.js 2 in order to bind values to a form component?

Currently, I am developing a basic form component that includes input fields for first name, last name, email, and phone number. I want to pass the user object to prefill the form and make changes using just one v-model. This is my initial code. <div& ...

Ways to avoid an infinite loop caused by onAuthStateChanged Firebase Authentication

I have a provider setup that initializes the authentication context from Firebase Auth. Everything is working well, but when I tried to introduce persistence by adding an observer with onAuthStateChanged, it caused an infinite loop. Despite including an u ...