PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 16, 2022

[FIXED] Why is my disabled prop not working as expected?

 October 16, 2022     html, javascript, typescript, vue.js, vuejs3     No comments   

Issue

In my Vue3 Project I have a form for registering a new user.

Said form contains a password input, a confirm password input and a save button.
Both input fields use v-on:input to trigger a typescript function called comparePasswords. In there both input values are compared and if they are equal, should enable the save button (and else disable it again).

The button uses :disabled="!passwordsMatch", which is just a variable that is set by the comparePasswords function, and by default is set to false.

When I type in matching passwords and log the result, the function does its job and passwordsMatch is true. However the button is not activated by it, seemingly does not refresh.

Solutions I could find online are using :disabled pretty much in the same way.

So how do I have my button enabled/disabled with the result of my comparePasswords function?

Here's the relevant code:

<template>
    <input v-model="adminPass" placeholder="Password" type="password" v-on:input="comparePasswords"/>
    <input v-model="adminPassVerification" placeholder="Confirm Password" type="password" v-on:comparePasswords/>
    <button :disabled="!passwordsMatch">Save</button>
</template>

<script setup lang="ts">
    import {ref} from 'vue'

    //Getting the data from input
    var adminPass = ref('')        
    var adminPassVerification('')

    var passwordsMatch: boolean = false

    function comparePasswords() {
        if(adminPass.value != '') { //Checks if input is empty
            passwordsMatch = adminPass.value == adminPassVerification.value //Updates passwordsMatch
            console.log(passwordsMatch)
        }
    }
</script>

Solution

This code works as shown here

<template>
  <input v-model="adminPass" placeholder="Password" type="password" @input="comparePasswords" />
  <input v-model="adminPassVerification" placeholder="Confirm Password" type="password" @input="comparePasswords" />
  <button :disabled="!passwordsMatch">Save</button>

  <p>{{ passwordsMatch }}</p>
</template>

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

const adminPass = ref('')        
const adminPassVerification = ref('')
const passwordsMatch = ref(false)

function comparePasswords() {
  if(adminPass.value) {
    passwordsMatch.value = adminPass.value === adminPassVerification.value
    console.log(passwordsMatch)
  }
}
</script>


Answered By - kissu
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing