使用vue写一个前端注册页面 要求有用户名 密码 年龄 性别 电话 头像 密码不得超过超过6位 年龄不得超过正常值 电话不得超过11位并且合法 名字要有敏感词汇的过滤,若有敏感词汇给予提示
时间: 2024-05-04 20:16:52 浏览: 8
好的,这里给出一个简单的示例代码,供参考:
```
<template>
<div class="register-form">
<div class="form-group">
<label for="username">用户名:</label>
<input type="text" id="username" v-model="username" />
</div>
<div class="form-group">
<label for="password">密码:</label>
<input type="password" id="password" v-model="password" />
</div>
<div class="form-group">
<label for="age">年龄:</label>
<input type="number" id="age" v-model="age" />
</div>
<div class="form-group">
<label>性别:</label>
<input type="radio" id="male" value="male" v-model="gender" />
<label for="male">男</label>
<input type="radio" id="female" value="female" v-model="gender" />
<label for="female">女</label>
</div>
<div class="form-group">
<label for="phone">电话:</label>
<input type="tel" id="phone" v-model="phone" />
</div>
<div class="form-group">
<label for="avatar">头像:</label>
<input type="file" id="avatar" accept="image/*" @change="handleAvatarChange" />
</div>
<div class="form-group">
<button @click="submitForm">提交</button>
</div>
<div class="form-group">
<p v-if="error">{{ error }}</p>
</div>
</div>
</template>
<script>
export default {
data() {
return {
username: '',
password: '',
age: null,
gender: '',
phone: '',
avatar: null,
error: ''
}
},
methods: {
handleAvatarChange(event) {
this.avatar = event.target.files[0]
},
submitForm() {
// 检查用户名是否包含敏感词汇
const sensitiveWords = ['敏感词1', '敏感词2', '敏感词3']
for (const word of sensitiveWords) {
if (this.username.includes(word)) {
this.error = '用户名包含敏感词汇'
return
}
}
// 检查密码是否超过6位
if (this.password.length > 6) {
this.error = '密码不能超过6位'
return
}
// 检查年龄是否超过正常值
if (this.age > 100 || this.age < 0) {
this.error = '年龄不合法'
return
}
// 检查电话是否合法
const phoneRegex = /^[1][3,4,5,7,8][0-9]{9}$/
if (!phoneRegex.test(this.phone)) {
this.error = '电话号码不合法'
return
}
// 在这里可以将表单数据提交到后端进行处理
this.error = ''
}
}
}
</script>
<style>
.form-group {
margin-bottom: 20px;
}
</style>
```
这是一个基本的注册表单,包括用户名、密码、年龄、性别、电话、头像等字段。其中,敏感词汇的检查、密码长度、年龄范围、电话号码合法性的检查都是通过 JavaScript 在前端进行的。在提交表单时,可以将数据发送到后端进行进一步的处理和验证。