The process of filtering a model stops at the third character and results in an empty output

Whenever I input a value into the 'temp_dollars' model and use a watch property to filter it, the input only retains the first 3 characters and resets.

For instance: When I type 123, The model value remains as 123. But when I type 1234, It changes to 0.


Vue.filter('to_currency', (val) => {
    let string = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'PHP' }).format(val).toString()
    return string.substr(4, string.length - 7)
})

watch: {
    temp_dollars (val) {
        this.temp_dollars = this.$options.filters.to_currency(val)
        console.log(this.temp_dollars)
        console.log(this.$options.filters.to_currency(val))
    }
}
    

I expected that any input in the text field would automatically be formatted so that 1234 appears as 1,234.

** UPDATE **

This is my Home.vue

Answer №1

I'm not entirely sure if I am addressing the specific question you posed, but it appears that the issue at hand revolves around formatting a number as currency and then eliminating the currency symbol along with the decimal digits.

If this is indeed what you are trying to solve, the solution lies in adjusting your formatting settings as follows:

{
  style: "decimal",
  currency: "PHP",
  minimumFractionDigits: 0
}

By implementing these options, you will receive 1,234 instead of PHP 1,234.00.

Furthermore, regarding the problem you mentioned where the value turns to 0 upon reaching 1234, it appears that this occurs because you are setting the edited value to match the formatted value. This practice should be avoided, as the formatted value is meant for display purposes only.

If you wish to create an illusion for the user that they are typing while the number is being formatted, you can explore approaches such as the following:

Experience a demonstration of a hidden input here

Answer №2

I finally found the solution, but somehow I neglected to share it earlier. The trick is to filter your v-model upon inputting data. For instance, if you initially input '123' and then add '4', a comma will be automatically added which makes it a NaN. In order to fix this issue, you need to filter out the commas in your computed property.

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

Why is my JavaScript if statement not functioning properly when paired with an else statement

Currently, I am in the process of developing a game and have encountered an issue with implementing a new item for purchase. Despite having all the necessary components in place, the code seems to be skipping over the function unexpectedly. function buy ...

Transitioning to Firebase Authentication

I made the decision to transition away from firebase authentication. To accomplish this, I exported all firebase users along with their email addresses, hashed passwords, salt keys, and other necessary information. Next, I transferred them to a database a ...

What is the benefit of using $q.all() with a single promise in AngularJS?

As I delve into this codebase, one snippet keeps popping up: $q.all([promise]).then(responseFunc); However, I find this confusing. After reading the documentation, I wonder why not simply use the following, as it's just a single promise... promise. ...

Changing the color of placeholder text in MUI 5 TextField

Looking to customize the text color and placeholder text color in my MUI TextField component to be green https://i.sstatic.net/NZmsi.png The documentation doesn't provide clear instructions, so I attempted a solution that didn't work: <TextF ...

Notify immediately if there is any clicking activity detected within a designated div container

I am looking to trigger an alert when a specific div is clicked. Here is the scenario: <div class="container"> <div class="header"> <h1>Headline<h1> </div> <div class="productbox"></div> </div> I have succ ...

Confirm the presence of Cookie and save the data

I'm a beginner in the world of Javascript and Ajax, attempting to save a user's name using cookies. I have created a form where users can input their first name (identified by id = firstName). My goal is to remember this information so that the n ...

What is the process of making circle or square shapes with text inside using React, without the use of SVG images?

Is there a way to create circle and square shapes in React with custom text inside, without relying on SVG images? Check out this example: I attempted the code below but it did not render any shapes: import React from 'react'; export default fu ...

Trying out the fetch api with Jest in a React Component: A step-by-step guide

As a newcomer to test driven development, I stumbled upon a section that talked about testing/mocking a fetch API. However, I am facing issues while trying to write my own test. In order to practice this concept, I created a simple weather app where I atte ...

JavaScript: Can you clarify the value of this variable using five sets of double quotations?

Could you please review the code snippet below for me? <script type="text/javascript"> function recentpostslist(json) { document.write('<ul class="recommended">'); var i; var j; for (i = 0; i < json.feed.entry.length; i++) { ...

JavaScript function following AJAX request

I'm attempting to invoke a JavaScript function that is returned in an Ajax call What is the proper way to run a JavaScript function received from an Ajax call? Consider this script before the Ajax call <script> function start(){ console.l ...

Is it feasible for JSON.stringify to maintain functions in its serialization?

Consider this item: x = { "key1": "xxx", "key2": function(){return this.key1} } Suppose I execute the following code: y = JSON.parse( JSON.stringify(x) ); After running the above code, y will contain { "key1": "xxx" }. Is there a way to include funct ...

Integrate Thymeleaf properties seamlessly into JavaScript code

I am attempting to embed a property from Spring's application.properties into JavaScript. It is working properly with the following code: <h1 th:utext="${@environment.getProperty('key')}"></h1> However, it returns null with th ...

Storing deeply nested arrays of objects with Mongoose leads to MongoDB storing empty objects

Here's the issue I'm facing: I am trying to save nested objects with mongoose in mongodb, but when I save them, the nested objects end up empty. I have attempted various solutions found online, such as pushing objects and populating, but none o ...

Sending a javascript variable to an angularjs scope

Currently, I am utilizing Flask to render an HTML template and wish to transfer the variable add_html_data, which is passed via Flask's render_template, to the scope of an AngularJS controller. I have attempted the following: <body> <di ...

JavaScript client receives a response from the server

After filling out an HTML form and submitting it, the client validates the data (such as checking if the EULA checkbox is accepted) before sending it to the server. The server then checks the data and returns a status code. But how can I retrieve this stat ...

Vue templates cannot access global variables

In this scenario, using the {{ window }} template results in a [Vue warn]: Property or method "window" is not defined Is this the expected behavior? It seems that defining window in the Vue data would not be recommended. ...

Looking to spice up your static HTML site? Dive into the world of Ruby on

I recently developed an app that features a static HTML webpage containing text and images, and now I'm interested in incorporating Ruby on Rails to explore its capabilities further. After creating a basic RoR application, I copied the HTML content f ...

Display various images based on different device orientations in UIWebView

Is there a way to display varying images in my HTML on a UIWebView, depending on the orientation of an iPhone? (I apologize if this question seems basic...) ...

What method can be used to keep Chromium open while using Puppeteer?

I am trying to figure out how to launch only one instance of Chromium from my first script and then connect to it from subsequent scripts. I am aware of the puppeteer.connect() method, but the issue is that when I start the script responsible for launching ...

What steps can you take to resolve the "TypeError: Cannot read property 'id' of undefined" issue?

I have been developing an app that involves using databases to add items for users based on their user ID, which is their username. However, whenever I attempt to add an item, I encounter an error that I can't seem to troubleshoot. The error message r ...