密码由6~20位英文、数字或下划线组成
时间: 2024-03-17 15:39:39 浏览: 25
在 Vue.js 中,你可以使用正则表达式来检查密码是否符合要求。因此,你可以在校验密码的方法中添加以下代码:
```html
<template>
<form>
<label for="password">Password:</label>
<input type="password" id="password" v-model="password">
<br>
<label for="password-confirm">Confirm Password:</label>
<input type="password" id="password-confirm" v-model="passwordConfirm">
<br>
<button @click.prevent="submit">Submit</button>
</form>
</template>
<script>
export default {
data() {
return {
password: '',
passwordConfirm: ''
}
},
methods: {
submit() {
// Check if password is empty
if (this.password === '') {
alert('Password cannot be empty!')
return
}
// Check if password length is between 6 and 20 characters
if (this.password.length < 6 || this.password.length > 20) {
alert('Password length must be between 6 and 20 characters!')
return
}
// Check if password contains only English letters, numbers, or underscores
const pattern = /^[a-zA-Z0-9_]+$/
if (!pattern.test(this.password)) {
alert('Password can only contain English letters, numbers, or underscores!')
return
}
// Check if password and password confirm match
if (this.password !== this.passwordConfirm) {
alert('Passwords do not match!')
return
}
// Password is valid, submit form
alert('Password is valid, submitting form...')
}
}
}
</script>
```
在上面的代码中,我们使用了正则表达式 `^[a-zA-Z0-9_]+$` 来匹配密码是否只包含英文、数字或下划线。如果密码中包含其他字符,将会提示用户密码不符合要求。