How can values be populated from an input box in the background in Vue.js?

I'm working on a project that involves Vue.js and Quasar.

One of the requirements is an input field initialized with 10 zeros, only allowing numeric numbers. The catch is that when adding a number, it should fill in from behind.

"0000000000" -> add 1

"0000000001" -> add 2

"0000000012" -> add 5

"0000000125" -> delete the last character

"0000000012" -> add 3

"0000000123"

Do you have any ideas on how I can achieve this functionality? Here's what I have coded so far:

<template>
<div> 
<q-input class="input" v-model="number" outlined label="number" mask="##########" minLength="10" maxlength="10" @input="onInput"/>
</div>
</template>

<script>
import { ref } from 'vue'

export default {

setup() {
const number = ref("0000000000")

}
return {
number
}
}
</script>

The current onInput function doesn't have any behavior defined yet.

Answer №1

You are advised to utilize

mask="#########" fill-mask="0" reverse-fill-mask

It performs optimally with maxlength="x" and mask="#*(x-1) ( mask="#########") but not with mask="#*x

const { createApp, ref } = Vue;

const app = createApp({
  setup () {  
    const number1 = ref(0)
    const number2 = ref('0000000000')
    const onInput = (newVal) => {
      number2.value = ('000000000'+ newVal).slice(-10);
    }
    return { number1, number2, onInput }
  }
})

app.use(Quasar)
app.mount('#q-app')
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="59282c382a382b196b776868776e">[email protected]</a>/dist/quasar.prod.css" rel="stylesheet" type="text/css>
<div id="q-app">
Standard mask: <br/>
number1: {{number1}}
<q-input class="input" v-model="number1" outlined label="number" mask="##########" fill-mask="0" reverse-fill-mask minLength="10" maxlength="10" @update:model-value="onInput"></q-input><br>
Custom mask: <br>
number2: {{number2}}<br>
<q-input class="input" v-model="number2" outlined label="number" minLength="10"  @update:model-value="onInput"></q-input>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e3f3b2f3d2f3c0e7c607f7f6079">[email protected]</a>/dist/quasar.umd.prod.js"></script>

Answer №2

To easily add leading 0 characters until a specific string length, you can utilize the String.prototype.padStart function.

Simply adjust the v-model whenever there are changes.

<script setup>
import { ref, watch } from 'vue'

const counter = ref('')
watch(counter, () => {
  // Remove any leading 0 characters
  counter.value = counter.value.substring(Array.from(counter.value).findIndex(v => v !== '0'))
  // Add leading 0 characters if needed
  counter.value = counter.value.padStart(8, '0')
})

</script>

<template>
  <input v-model="counter" placeholder="Enter a number">
</template>

Check out this live demo

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

Integrate an item into the request body utilizing the $.load() function in jQuery

Currently, I am using a POST request via jQuery's $.load function to retrieve data to display in a window. This is what I am currently doing: var testObject = { thing1: 'data1', thing2: 'data2', thing3: &a ...

What is the process for updating the label name when a file is chosen?

Check out this HTML code snippet: <input type="file" class="custom-file-input" accept="image/*" onchange="loadFile(event)" id="customFile"> <label class="custom-file-label" id="chan ...

Showcase a sizable picture broken down into smaller sections

I am interested in creating a mapping application similar to Google Maps that can asynchronously load images from the backend. I am seeking guidance on where to begin and how to proceed in this endeavor. The ultimate goal is to have the image displayed w ...

Regularly verify if a date has passed its expiration

Consider our situation: Our web app utilizes node with express, requiring a background process to monitor the creation dates of multiple posts and trigger specific events when they expire. These expiration dates are user-defined, leading to varying expira ...

Discover the current URL using Gridsome

I am currently working on a Gridsome project and I want to enhance the menu by adding the active CSS class to highlight the current URL. In order to achieve this, I need to retrieve the current URL path within the Layout component so that I can compare it ...

The error message "Each item within a list must be assigned a unique 'key' prop" is being displayed

At the moment, I'm immersed in a project that utilizes React, Next.js, and Ant-Design. However, during the development process, I encountered an error due to the absence of a unique key like so: Here's the detailed log of the error: Warning: Ea ...

Can you explain what is meant by an "out of DOM" element?

I'm feeling a bit lost when it comes to DOM nodes and all the terminology surrounding them. Initially, I believed that the DOM consisted solely of what I could see in my inspector - nothing more, nothing less. However, I've come across functions ...

Receiving unique socket IDs for two different socket.on events

Currently, I am in the process of developing a socket-based application using React and Node/Express. As part of this project, there is an event listener set up for when a user connects with io.on('connection'). Upon connection, the user is added ...

Having issues with connecting the front-end (Vue) to the API (Node.js), utilizing nginx on an EC2 AWS instance running Ubuntu 20.04

I am currently in the process of setting up web servers. Initially, I decided to use NodeJS with Express for backend, specifically listening on port 3002. app.js is now successfully listening at port 3002 Subsequently, I configured the Ubuntu firewall to ...

Error: Attempting to access a property of an undefined value, please verify the key name is

Attempting to incorporate dynamic elements into the assignment, I have written the following code: var contentList = Object.keys(content)[0]; $scope.model.currentLoadedContent[contentList] = content[Object.keys(content)[0]] When trying to execute the seco ...

Error encountered with the {1}+ possessive quantifier in a JavaScript regex pattern

As I delve into learning Javascript and Express.js simultaneously, I decided to experiment with regular expressions while making a get request. To familiarize myself with regular expressions, I referred to this chart (also shown below). Greedy Reluctant ...

Identifying the empty population of a hasMany property

I'm facing a challenge that seems simple, but I'm struggling to find the solution. Currently, I'm working with Emberjs 2.8. In my model, I have an async: true property set up like this: models/my-model.js import DS from 'ember-data& ...

Utilizing JavaScript to Parse RSS XML Feeds Across Domains

Looking to parse an RSS (XML) file without relying on the Google RSS feed. I have attempted using JSONP, but it resulted in downloading the file and throwing an error "Uncaught SyntaxError: Unexpected token < ". $.ajax({ type: "GET", ur ...

Encountering a problem with loading popper.js in Bootstrap 4 while utilizing require.js for loading

After installing bootstrap 4, jquery, and popper via npm, I encountered an error in the console when loading my website. The error indicated that the file popper.js could not be retrieved. Upon further inspection, I found that there were two instances of p ...

Struggling to traverse through intricate layers of nested objects in React and showcasing them without going crazy

Dealing with an API that returns data structured in deeply nested objects has been a challenging task. The goal is to extract specific data from these nested objects and then display them within a React project. Despite numerous attempts, finding a simple ...

Tips for iterating over two models using ng-repeat

I'm a newcomer to Angular and I have an issue that requires a neat solution: I have a div element called "SelectedList" that needs to display a list of active elements from another container. This container contains two tabs, Tab1 and Tab2, which are ...

What is the best way to transform the words in my JavaScript array into clickable links?

I am working on a search bar that autofills words and I want to turn those autofilled words into clickable links. For example, if I type "locations" in the search bar, I want it to autofill the word "locations" and turn it into a link that directs me to pa ...

What is the best way to automatically add a space every four digits in an input field using JavaScript?

How can I format a card number input field so that it displays spaces after every 4 digits when the customer enters the full 16-digit number? <input type="text" id="input-number" placeholder="e.g 1234 5678 9123 0000"> ...

Problem with clicking on items in Slick Slider Variable width menu once they reach the end

Is there a way to stop the slick slider with variable width items from scrolling after reaching the last item? I am encountering this issue where it continues to scroll even after the end. If you want to see the problem in action, check out this fiddle: h ...

transform JSON structure into an array

Is it possible to convert an interface class and JSON file into a list or array in order to work on it? For example, extracting the Racename from each object in the JSON file and storing it in a list/array. Here is the interface structure: interface IRunn ...