gpt4 book ai didi

javascript - 在 vuejs 中使用 vuelidate 验证密码?

转载 作者:行者123 更新时间:2023-12-05 06:03:18 32 4
gpt4 key购买 nike

我从 How to validate password with Vuelidate? 得到了引用

 validations: {

user: {
password: { required,
containsUppercase: function(value) {
return /[A-Z]/.test(value)
},
containsLowercase: function(value) {
return /[a-z]/.test(value)
},
containsNumber: function(value) {
return /[0-9]/.test(value)
},
containsSpecial: function(value) {
return /[#?!@$%^&*-]/.test(value)
},


minLength: minLength(8),maxLength: maxLength(19) },
confirmPassword: { required, sameAsPassword: sameAs("password") },
},
}
<input
:type="passwordFieldType"
v-model="user.password"
v-model.trim="$v.user.password.$model"
id="password"
name="password"
class="input-section-three-login"
:class="{'is-invalid': validationStatus($v.user.password) }"
placeholder="Enter new password"
:maxlength="maxpassword"
v-on:keypress="isPassword($event)"

/>


<button v-on:click="registerMe" :disabled="user.confirmPassword != user.password " :class="(isDisabled) ? '' : 'selected'" > Register
</button>

如何使用 vuelidate 验证密码,我在 validationStatus 中有正则表达式值,在按钮上单击如何检查正则表达式验证。

尝试使用正则表达式验证密码是否成功,移动到下一个屏幕,否则直到成功他需要留在当前屏幕。

最佳答案

尝试以下步骤来解决问题

第 1 步:验证 修改如下

validations: {
user: {
password: { required,
valid: function(value) {
const containsUppercase = /[A-Z]/.test(value)
const containsLowercase = /[a-z]/.test(value)
const containsNumber = /[0-9]/.test(value)
const containsSpecial = /[#?!@$%^&*-]/.test(value)
return containsUppercase && containsLowercase && containsNumber && containsSpecial
},
minLength: minLength(9),
maxLength: maxLength(19),
},
confirmPassword: { required, sameAsPassword: sameAs("password") },
},
},

第二步:RegisterMe按钮点击事件

methods: {
registerMe() {
this.submitted = true;
this.$v.$touch();
if (this.$v.$invalid) {
return false; // stop here if form is invalid
} else {
alert("Form Valid. Move to next screen");
}
},
},

第 3 步:计算 函数以禁用Register 按钮

computed: {
isDisabled() {
return this.$v.$invalid;
},
},

第 4 步:HTML 模板是这样的

<form @submit.prevent="registerMe" novalidate>
<div class="form-group">
<input
type="password"
class="form-control"
placeholder="Enter Password"
value=""
v-model="user.password"
autocomplete="off"
/>
<div
v-if="this.submitted && $v.user.password.$error"
class="invalid-feedback left">
<span v-if="!$v.user.password.required">Password is required</span>
<span v-if="user.password && !$v.user.password.valid">Password contains atleast One Uppercase, One Lowercase, One Number and One Special Chacter</span>
<span v-if="user.password && $v.user.password.valid && !$v.user.password.minLength">Password must be minimum 9 characters</span>
<span v-if="user.password && !$v.user.password.maxLength">Password must be maximum 19 characters</span>
</div>
</div>
<div class="form-group">
<input
type="password"
class="form-control"
placeholder="Confirm Password"
value=""
v-model="user.confirmPassword"
autocomplete="off"
/>
<div
v-if="this.submitted && $v.user.confirmPassword.$error"
class="invalid-feedback left"
>
<span v-if="!$v.user.confirmPassword.required"
>Confirm Password is required</span
>
<span
v-if="
user.confirmPassword && !$v.user.confirmPassword.sameAsPassword
"
>Password and Confirm Password should match</span
>
</div>
</div>
<input
type="submit"
class="btnRegister"
value="Register"
:disabled="this.isDisabled"
/>
</form>

DEMO

关于javascript - 在 vuejs 中使用 vuelidate 验证密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66688532/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com