Issue with Vue.js input not updating with v-model after input sanitization in watch handler

Recently, while working with Vue 2.6, I came across an unusual issue when trying to sanitize user input.

The main culprit seemed to be a custom component that housed the input field. Here's a simplified version of it:

<template>
  <input :name="name" :value="value" @input="(e) => onInput(e.target.value)" />
</template>

The onInput function simply emits the input event and both value and name are passed as props.

This component worked smoothly in most scenarios until I needed to filter out lowercase and whitespace characters from the input. To achieve this, I set up a watcher for the field:

onFieldChange(field) {
  this.field = field.replace(/[\sa-z]/gi, '')
}

After implementing this, I noticed a strange behavior where entering an invalid string like test would appear as expected inside the input field, but upon closer inspection, the actual field value was attached as an empty string due to filtering out the invalid characters. This discrepancy became more apparent when adding a valid character, like T, resulting in the correct rendering as just T.

It dawned on me that the root cause lay in the fact that my Field component failed to recognize any changes in its props ('value' remained constant as an empty string), thus not triggering a re-render. Despite identifying the issue, I'm unsure about how to address it. Does anyone have any insights or possible solutions? Your help is much appreciated. Thanks!

Answer №1

Make sure to update the value of your field using nextTick.

onFieldChange(field) {
  this.$nextTick(()=>{
    this.field = this.field.replace(/[\sa-z]/gi, '')
  })
  // this.field = field.replace(/[\sa-z]/gi, '')
}

Check out the jsfiddle here

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

Scrolling automatically to the first empty mandatory field with the help of AngularJS

After utilizing angular js to create a form with 7 input elements, I encountered an issue. Upon clicking submit, the form should scroll up to the first blank required field. However, it is not accurately identifying the left blank field. Any suggestions on ...

When using useEffect in React, the handleKeyDown function is causing the output to double with each

Whenever a user inputs something, the output doubles each time, leading to a long loop after just a few inputs. Below is the code snippet: const [direction,setDirection] = useState(Array(1).fill(0)); const directions = ["botNorth","botEa ...

Utilizing React Native to seamlessly integrate one JavaScript function into another

I am trying to integrate one javascript module into another. I recently downloaded a demo of GiftedMessanger After downloading the GiftedMessanger demo code, I incorporated all its dependencies into my own React Native (ios) project and successfully insta ...

Configure markers on Google Maps

I've been attempting to integrate my markers into Google Maps, but I'm stuck on how to use the following: map.fitBounds(latlngbounds); Any ideas on how I can implement this easily? Below is the code I have so far: <script type="text/javas ...

Unable to download and install jspdf version 1.5.3

Currently, I am facing an issue where I need to convert HTML to PDF using jspdf 1.5.2. However, I am encountering an error that says "Cannot read property 'charAt' of undefined" when trying to utilize html2canvas. When attempting to upgrade to j ...

An element in AngularJS is replicated for a brief moment of 1 second

I am having trouble with my login form that displays an error message in a box. <div class="ui negative message" ng-if="vm.message != ''"> <span ng-bind="vm.message"></span> </div> The error message is being set in t ...

My CSS seems to be causing an issue and preventing the function from running correctly. Now I just need to

I'm currently working on a project and following a tutorial to help me create a navigation bar. The tutorial I am using can be found here: https://www.youtube.com/watch?v=gXkqy0b4M5g. So far, I have only reached the 22:05 mark in the video. I have su ...

Tips for disabling drag slide functionality in Vue with agility

I am working with vue-agile and encountering an issue where videos are causing the slider to move unexpectedly when dragging the player's pointer. Images display fine, but this drag/swipe behavior is disrupting the user experience. Is there a way to d ...

Utilizing various filters and sorting options on API response within Angular 8

Upon receiving the following API response: [ { "imgPaths":[ "gallery/products/55ccb60cddb4d9bded02accb26827ce4" ], "_id":"5f3e961d65c6d591ba04f3d3", "productName":" ...

Custom HTML binding in expanding rows of Angular 2 DataTables

I am currently working on implementing a data table feature that allows for an extended child row to be displayed when clicking the + icon. This row will show additional data along with some buttons that are bound via AJAX before transitioning to Angular 2 ...

Utilize Vue and Django to submit dynamic form data efficiently

Currently utilizing Vue.js to send data from a standard .html form to Django. My experience with Python and Django is relatively fresh, only a few days in. Within my form, there is a segment where users can dynamically input form fields (both regular inpu ...

Guidelines on declining a pledge in NativeScript with Angular 2

I have encountered an issue with my code in Angular 2. It works fine in that framework, but when I tried using it in a Nativescript project, it failed to work properly. The problem arises when I attempt to reject a promise like this: login(credentials:Cr ...

Passing data from a child component to a parent component in Vue 3: How

Struggling with Vue 3 app authentication through event-emission from child to parent. Below is a snippet of the code: Child <template> <form class="msform"> <input @click="goToLogin" type="button" name=&q ...

When trying to implement appDir and withPWA in next.config.js, an error has been encountered

My next.config.js is set up with next-pwa and an experimental app feature included. const withPWA = require('next-pwa'); module.exports = withPWA({ pwa: { dest: 'public', disable: process.env.NODE_ENV === 'development&ap ...

Unable to bypass YouTube advertisement

I am currently experimenting with using nodejs puppeteer to test if I can bypass the ad on Youtube. Although this is just for testing purposes, I am facing some challenges with getting it to work as expected. I have implemented a while loop to search for ...

Why is the parameter declared if its value is never even used? Seems redundant

When trying to send an email, I've encountered an issue with my controller method: const send = function (subject, template, to, options) { // While VSC points out that "subject" is declared but its value is never read, // it does not signal ...

The Quivering Quandaries of Implementing Jquery Accordions

For a demonstration of the issue, please visit http://jsbin.com/omuqo. Upon opening a panel by clicking on the handle, there is a slight jitter in the panels below during the animation. In the provided demo, all panels should remain completely still as t ...

JavaScript inheritance through prototypes and the properties of objects

I am currently exploring the concept of prototyped inheritance in JavaScript for a function. This process is well documented in Wikipedia's javascript article. It functions smoothly when dealing with simple JavaScript types: function Person() { t ...

Integrate PHP include files into a website without allowing direct access, then dynamically load them using jQuery AJAX

I am utilizing jQuery AJAX to load the content of about.php page into my index.php section in this manner: $('.nav a[href="#about"]').on('click', function() { $('main').load('../../layout/about.php'); }); The iss ...

Unusual behavior exhibited by dynamic code in iframe

When trying to retrieve a dynamic height iframe, I implement the code below. In the <head> area <script language="javascript" type="text/javascript"> function adjustIframe(obj) { obj.style.height = obj.contentWindow.document.body.scrollHeight ...