Vue JS Vee验证密码确认始终为假

时间:2018-07-10 12:38:33

标签: validation vue.js vuejs2 vee-validate

我正在尝试使用vee validate通过此代码验证密码。

<div>
    <input type="password"
           placeholder="Password"
           v-model="password"
           v-validate="'required|min:6|max:35|confirmed'"
           name="password" />
</div>
<div>
    <span>{{ errors.first('password') }}</span>
</div>
<div>
    <input type="password"
           placeholder="Confirm password"
           v-model="confirmPassword"
           v-validate="'required|target:password'"
           name="confirm_password" />
</div>
<div>
    <span>{{ errors.first('confirm_password') }}</span>
</div>

但是它总是显示密码确认不匹配。 我的代码出了什么问题?

任何帮助

谢谢。

2 个答案:

答案 0 :(得分:21)

Vee Validate语法出错,将target:更改为confirmed:

v-validate="'required|target:password'"

应该是

v-validate="'required|confirmed:password'"

此外,您的密码输入必须具有ref="password"-vee-validate会找到目标的方式

<input v-validate="'required'" ... ref="password">(谢谢Ryley)。

  

已确认:{target}-输入值必须与目标值相同   输入,由{target}指定为目标字段的名称。

看看下面的基本示例,它将检查两件事:

  • 第二个输入字段是否有任何输入值?
  • 如果是,则第二个输入值与第一个输入值匹配吗?

var app = new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue!'
  }
})
body {
  background: #20262E;
  padding: 15px;
  font-family: Helvetica;
}

#app {
  width: 60%;
  background: #fff;
  border-radius: 10px;
  padding: 15px;
  margin: auto;
}
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vee-validate/2.1.1/vee-validate.js"></script>
<script>
  Vue.use(VeeValidate);
</script>


<div id="app">

  <form id="demo">

    <!-- INPUTS -->
    <div class="input-group">
      <h4 id="header"> Enter Password</h4>

      <div class="input-fields">
        <input v-validate="'required'" name="password" type="password" placeholder="Password" ref="password">

        <input v-validate="'required|confirmed:password'" name="password_confirmation" type="password" placeholder="Password, Again" data-vv-as="password">
      </div>
    </div>

    <!-- ERRORS -->
    <div class="alert alert-danger" v-show="errors.any()">
      <div v-if="errors.has('password')">
        {{ errors.first('password') }}
      </div>
      <div v-if="errors.has('password_confirmation')">
        {{ errors.first('password_confirmation') }}
      </div>
    </div>

  </form>
</div>

进一步阅读:https://baianat.github.io/vee-validate/guide/rules.html#confirmed

答案 1 :(得分:4)

以下代码对我有用:https://logaretm.github.io/vee-validate/advanced/cross-field-validation.html#targeting-other-fields

<template>
  <ValidationObserver>
    <ValidationProvider rules="required|password:@confirm" v-slot="{ errors }">
      <input type="password" v-model="password" />
      <span>{{ errors[0] }}</span>
    </ValidationProvider>

    <ValidationProvider name="confirm" rules="required" v-slot="{ errors }">
      <input type="password" v-model="confirmation" />
      <span>{{ errors[0] }}</span>
    </ValidationProvider>
  </ValidationObserver>
</template>

<script>
import { extend } from 'vee-validate';

extend('password', {
  params: ['target'],
  validate(value, { target }) {
    return value === target;
  },
  message: 'Password confirmation does not match'
});

export default {
  data: () => ({
    password: '',
    confirmation: ''
  })
};
</script>