【问题标题】:How to validate password field with every character in Vuejs?如何使用 Vuejs 中的每个字符验证密码字段?
【发布时间】:2021-04-15 14:00:05
【问题描述】:

这是我的代码笔链接https://codepen.io/santoshch/pen/bGgKNWV

代码已经在验证两个密码字段,如果输入不匹配,则显示为必须相同。

但是需要它例如我在密码字段中输入“asdfg”,如果我在确认密码字段中输入“asddd”,那么在确认密码字段中,第 4 个字符与密码字段错误,然后应该显示错误消息比如“必须相同”。

尝试了长度,但不知道如何开始。

 <label class="form__group is-required">
    <span class="form__label">Password</span>
    <input
      class="form__input" type="password" name="form-password"
      v-model="password" @input="$v.password.$touch"
    >
    <p v-if="$v.password.$dirty">
      <span class="form__alert" v-if="!$v.password.required">Required.</span>
      <span class="form__alert" v-if="!$v.password.minLength">
        {{$v.password.$params.minLength.min}} letters at least.
      </span>
    </p>
  </label>
  <!-- Repeat Password -->
  <label class="form__group is-required">
    <span class="form__label">Repeat<br>password</span>
    <input
      class="form__input" type="password" name="form-repeat-password"
      v-model="repeatPassword" @input="$v.repeatPassword.$touch"
    >
    <p v-if="$v.repeatPassword.$dirty">
      <span class="form__alert" v-if="!$v.repeatPassword.required">Required.</span>
      <span class="form__alert" v-if="!$v.repeatPassword.sameAsPassword">
        Must be identical.
      </span>
    </p>
  </label>

【问题讨论】:

    标签: javascript vue.js vuelidate


    【解决方案1】:

    将@change 放在您的 repeatPassword 输入中:

    v-model="repeatPassword" 
    @input="$v.repeatPassword.$touch" 
    @change="comparePasswords" // Add this trigger
    

    并添加一个方法:

    comparePasswords: function () {
    
      for (var i = 0; i < this.repeatPassword.length; i++) {
        if(this.password.charAt(i) != this.repeatPassword.charAt(i)) {
          alert("Char at pos " + (i + 1) + " does not match");
        }
      }
    
    }
    

    它会逐个字符比较。

    【讨论】:

    • 感谢您的回答。我已将上述代码添加到 codepen。但这并没有反映出任何变化。如果可能,请查看我的codepen.io/santoshch/pen/bGgKNWV
    • 好吧,使用 Vuelidate ,有没有其他方法可以做到这一点...我将代码添加到我的 codepen,它不需要 :)
    • 有没有人提供上述问题的解决方案。
    • @taneerudhanunjay 我不明白问题出在哪里。您的代码正在运行。如果确认的密码与密码不匹配,您会收到消息“必须相同”。这不是你想要的吗?
    • @RGriffiths,Ya 代码运行良好,但需要为 confirmPassword 文件添加一项功能。即,在确认密码字段中,我需要检查每个带有密码字段的字符,然后我需要显示我们输入错误的字符。
    猜你喜欢
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 2023-04-07
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    相关资源
    最近更新 更多