简体   繁体   English

如何在 vanilla Vue.js 中检查密码匹配?

[英]How to check passwords match in vanilla Vue.js?

I'm new to Vue.js and I'd like to check if passwords are matched.我是 Vue.js 的新手,我想检查密码是否匹配。 If they do not match, after the user leaves the confirmation field, the error text Passwords don't match!如果它们不匹配,则在用户离开确认字段后,错误文本Passwords don't match! should appear.应该出现。 I've seen a couple of solutions which involve using plugins, but I'm wondering what is the idiomatic way to do it using pure vue.js?我见过一些涉及使用插件的解决方案,但我想知道使用纯 vue.js 的惯用方式是什么?

https://jsfiddle.net/Babrz/L2md63j7/3/ https://jsfiddle.net/Babrz/L2md63j7/3/

<div id="app">
          <form >
          <div class="form-group">            
            <input type="email" class="form-control" placeholder="Email">            
          </div>
          <br>
          <div class="form-group">
            <input type="password" class="form-control" v-model="password" placeholder="Password">
          </div> 
          <br>

         <div class="form-group">
            <input type="password" class="form-control" v-model="password2" placeholder="Confirm Passwrd">
          </div>
          <small v-if="showError">Passwords don't match!</small>
          <br>

          <div class="form-group">

            <input type="text" class="form-control" placeholder="Age">            
          </div>         

           <br>

          <button type="submit" class="btn login-btn">Register</button>
        </form>
</div>

new Vue({
  el: "#app",
  data: {
    email: '',
    password: '',
    password2: '',
    age: 0,
    showError: false    
  },
  methods: {
    toggle: function(todo){
        todo.done = !todo.done
    }
  }
})

It sounds like you want to use an onblur event to run a validation on the two password values.听起来您想使用onblur事件对两个密码值进行验证 A very basic implementation might look like this.一个非常基本的实现可能如下所示。

...

<input  v-on:blur="validate" type="password" class="form-control" v-model="password2" placeholder="Confirm Passwrd">
...

...
new Vue({
  el: "#app",
  data: {
    email: '',
    password: '',
    password2: '',
    age: 0,
    showError: false
      
    

  },
  methods: {
    toggle: function(todo){
        todo.done = !todo.done
    },
    validate: function() {
        console.log(this.password === this.password2)
    }
  }
})
...

https://v2.vuejs.org/v2/guide/events.html https://v2.vuejs.org/v2/guide/events.html

You can get a lot of help if you use something like validate.js to validate your passwords too.如果你也使用 validate.js 之类的东西来验证你的密码,你可以获得很多帮助。

http://validatejs.org http://validatejs.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM