validation - Vue js vee 验证密码确认总是错误的

标签 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>

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

最佳答案

您输入的密码必须有ref="password" - 这就是 vee-validate 找到目标的方式:

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

confirmed:{target} - Input must have the same value as the target input, specified by {target} as the target field’s name.

此外,您的 Vee Validate 语法有错误,更改 target:confirmed:

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

应该是

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

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

  • 第二个输入框有没有输入值?
  • 如果是,第二个输入值是否与第一个输入值匹配?

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

关于validation - Vue js vee 验证密码确认总是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51265450/

相关文章:

java - 在 Java 中以语言环境敏感的方式验证十进制数

wpf - DataGridCell Validation.ErrorTemplate 被忽略

javascript - (插件 commonjs) SyntaxError : Unexpected token when packaging vue component with rollup

javascript - 当数据变为空数组时,v-for 和 v-if 不能一起工作

javascript - 如何在 Backbone 形式整数文本字段上定义最小/最大验证?

php - 如何在遗留 PHP 项目中使用 symfony2 验证器组件?

vue.js - 防止 Vue 积极地重用 dom 元素

javascript - Vuejs 类型错误 : Cannot use 'in' operator to search for

vue.js - 使用项目和字段,列跨度为 2 的 Bootstrap-vue 表

javascript - Vue - 根据父数据的状态渲染组件