Troubleshooting Vue 3 password validation issue

Creating a registration form for a website using Vue 3. I have a method that checks the values of the password and confirm password fields and compares them. If they match, nothing happens. However, if they do not match, a red label appears, and the submit button is disabled. Although I have implemented this in Vue 3, there seems to be an issue where it sometimes displays that passwords are different even when they are the same.

<template>
    <div>
        <label for="password">Password</label>
        <input type="text" v-model="password" name="password" id="password" placeholder="••••••••" required="">
    </div>
    <div>
        <label for="confirm-password">Confirm Password</label>
        <input @keydown="confirmPassword" type="confirm-password" v-model="confirm" name="confirm-password" id="confirm-password" placeholder="••••••••" required="">
        <label for="confirm-password" v-if="invalidPasswords">Passwords do not match</label>
    </div>
    <button :disabled="submitDisabled" type="submit">Create Account</button>
</template>

<script>
    export default {
        name: "RegistrationView",
            data () {
              return {
                ...
                password: '',
                confirm: '',
                invalidPasswords: false,
                submitDisabled: false,
              }
          },
          methods: {
            confirmPassword() {
              if (this.password !== this.confirm){
                this.invalidPasswords = true
                this.submitDisabled = true
              } else {
                this.invalidPasswords = false
                this.submitDisabled = false
              }
            },
        },
    }
</script>

Screenshots: https://i.sstatic.net/3eOB1.png https://i.sstatic.net/ein5h.png https://i.sstatic.net/ein5h.png

Answer №1

To modify the functionality, adjust the @keydown event to the @input event:

<input @input="confirmPassword" type="confirm-password" v-model="confirm" name="confirm-password" id="confirm-password" placeholder="••••••••" required="">
    

Answer №2

To enhance your password confirmation functionality, consider using either @keyup or @input instead of @keydown:

<input @keyup="confirmPassword" type="confirm-password" v-model="confirm" name="confirm-password" id="confirm-password" placeholder="••••••••" required="">

OR

 <input @input="confirmPassword" type="confirm-password" v-model="confirm" name="confirm-password" id="confirm-password" placeholder="••••••••" required="">

Both options will function perfectly for this purpose.

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

Trouble linking storage to database on Firebase with Vue.js

I'm currently diving into my Vue project integrating firebase to create locations with specialized images and data. I've been following a tutorial but it seems a bit outdated, especially when trying to connect Firebase storage with the database. ...

Press the designated button located within a table's td element to retrieve the values of adjacent td elements

I need to implement a functionality where clicking a button within a <td> element will retrieve the values of other <td> elements within the same row (<tr>). Here is the current implementation: $(document).ready(function(){ ...

Troubleshooting issue in Typescript with useFieldArray relating to property name

I am encountering an issue with useFieldArray in TypeScript. The problem arises when trying to specify the controller's name as: name={test.${index}.firstname} The error message reads: controller.d.ts(18, 5): The expected type comes from property &ap ...

Transitioning my website to incorporate Angular JS

Recently, I have been exploring AngularJS and I am quite impressed with its features. I am currently developing a website using traditional methods (php, html, css, mysql, some jQuery) and I am considering transitioning to Angular. The reason for this chan ...

How is it that the function is returned first, even though variables are hoisted to the top of the function scope in this scenario?

The reason why the variable in the displayInstructor function is returning undefined even though it should be at the top of the scope due to hoisting. The answer should not be the variable's value because of this. function displayInstructor(){ re ...

Choosing to maintain an open server connection instead of regularly requesting updates

Currently, I am in the process of developing an innovative online presentation tool. Let's dive into a hypothetical situation: Imagine one person is presenting while another connects to view this presentation. >> How can we ensure that the vie ...

Setting a promise value for an object's method

Imagine this object I am constructing. $scope.formules = {}; $scope.formules = { label: 'some label', getYear: function() { // getData() is a promise $scope.getData().then(function(){ // Some code to fetch ...

Tips for enhancing the "Eliminate render-blocking resources" score on the Lighthouse report for Progressive Web App (PWA) optimization

Check out my first attempt at a PWA here: My goal is to achieve a perfect 100% in Lighthouse for all categories :) https://i.sstatic.net/9Ql9r.png Although I've read the "Learn More" page, I'm still struggling with how to implement Bootstrap C ...

Utilizing express.js alongside the native MongoDB driver: A comprehensive guide

I'm facing difficulties when attempting a simple mongoDB query from my express app: app.js var express = require('express'); var routes = require('./routes'); var user = require('./routes/user'); var http = re ...

Setting a property with a generic type array: Tips and tricks

Currently, I am working on implementing a select component that can accept an array of any type. However, I am facing some challenges in defining the generic and where to specify it. My project involves using <script setup> with TypeScript. Here is ...

using jQuery to display the image when clicked on

Is there a way to make an image display in full screen when clicking on its thumbnail? The images are hardcoded using the IMG tag and have the class .imagethumb. Currently, the images are displayed as thumbnails. When clicking on a thumbnail, I would lik ...

Interactive Card Matching Game featuring jQuery with a mix of images, numbers, and names

I recently added images to this Jquery Game, but now I'm facing a challenge. I want to match the numbers with their corresponding flower names (terms), for example, 1=Foglove. However, when I try to add terms in the code below, it randomizes both the ...

Having trouble getting v-validate to work on dynamically generated fields in Vue.js?

I am facing an issue with validating dynamic fields using v-validate. It seems to work fine for static fields, but the same code does not seem to validate dynamically generated fields: <div v-if="condition=='true'> <input ...

Conceal the div element when located beneath an ordered list with a designated

I need help hiding the display of comment information if it is a child comment. Below is my attempt to hide the div with the id "info" if the "ol" element has a class of "children". If you have another method for hiding the div when the comment is a chil ...

How can I effectively handle and store class instances while managing state within a react application?

I have a Rectangle class that I am using: class Rectangle { constructor(x, y, width, height, color, hasCollision = false, kills = false) { this.A = new Point(x, y) this.B = new Point(x + width, y) this.C = new Point(x + width, y + height) ...

Scaling Vuetify icons dimensions

During the development of an app using Vuetify, I encountered challenges in changing the default colors set by Vuetify. After some trial and error, I came across a solution on: https://github.com/vuetifyjs/vuetify/issues/299 The solution involved adding ...

Issues with applying jQuery's addClass and removeClass functions

Within a lengthy function, I am encountering the following scenario: if ( shipSet == true ) { $("#" + shippingFields[i]).style.background = 'gray'; $("#" + shippingFields[i]).className = 'optional'; } else { $("#" + s ...

What are the steps for setting up vue-cli on a server?

I have been researching how to install vue-cli. All the tutorials I found show it getting installed on the localhost:8080. Is it not possible to install it on a server? I attempted to install vue-cli on port 8080 on my server, but it ended up being on po ...

The function req.send('null') does not exist in the Node.js MySQL library

I am struggling with inserting a form into a MySql database. The values are stored in the database from the form, but the table displayed on the page does not refresh. I can only see the entries when I restart the server and revisit the page. Furthermore, ...

Re-sending keyup event for DATE input in JavaScript

Is there a way to redirect the keyup KeyboardEvent from one native date input element to another in real time, similar to typing in two date inputs simultaneously? I experimented with this code using a text input, and it worked perfectly. However, when I ...