feat: 用户名验证支持点号,统一错误消息,完善前端验证

- 后端正则改为 ^[a-zA-Z0-9_.\-]+$ 支持点号
- 统一错误消息使用"连字符"
- 前端 UserFormDialog 和 RegisterDialog 添加实时验证
- 输入框显示格式提示和错误信息
This commit is contained in:
LewisPen
2026-01-27 12:17:19 +08:00
parent e24534668a
commit 9fefa94134
5 changed files with 58 additions and 9 deletions

View File

@@ -135,7 +135,14 @@
required
disable-autofill
:disabled="isLoading"
:class="usernameError ? 'border-destructive' : ''"
/>
<p
v-if="usernameError"
class="text-xs text-destructive"
>
{{ usernameError }}
</p>
</div>
<!-- Password -->
@@ -367,6 +374,17 @@ const sendCodeButtonText = computed(() => {
return '发送验证码'
})
// 用户名验证
const usernameRegex = /^[a-zA-Z0-9_.\-]+$/
const usernameError = computed(() => {
const username = formData.value.username.trim()
if (!username) return ''
if (username.length < 3) return '用户名长度至少为3个字符'
if (username.length > 30) return '用户名长度不能超过30个字符'
if (!usernameRegex.test(username)) return '用户名只能包含字母、数字、下划线、连字符和点号'
return ''
})
const canSubmit = computed(() => {
// 基本信息:用户名和密码必填
const hasBasicInfo =
@@ -376,6 +394,12 @@ const canSubmit = computed(() => {
if (!hasBasicInfo) return false
// 用户名格式验证
if (usernameError.value) return false
// 检查用户名格式
if (usernameError.value) return false
// 如果需要邮箱验证,邮箱和验证都必须完成
if (props.requireEmailVerification) {
if (!formData.value.email || !emailVerified.value) {

View File

@@ -53,7 +53,20 @@
data-form-type="other"
required
class="h-10"
:class="usernameError ? 'border-destructive' : ''"
/>
<p
v-if="usernameError"
class="text-xs text-destructive"
>
{{ usernameError }}
</p>
<p
v-else
class="text-xs text-muted-foreground"
>
3-30个字符允许字母数字下划线连字符和点号
</p>
</div>
<div class="space-y-2">
@@ -468,13 +481,25 @@ const { isEditMode, handleDialogUpdate, handleCancel } = useFormDialog({
resetForm,
})
// 用户名验证
const usernameRegex = /^[a-zA-Z0-9_.\-]+$/
const usernameError = computed(() => {
const username = form.value.username.trim()
if (!username) return ''
if (username.length < 3) return '用户名长度至少为3个字符'
if (username.length > 30) return '用户名长度不能超过30个字符'
if (!usernameRegex.test(username)) return '用户名只能包含字母、数字、下划线、连字符和点号'
return ''
})
// 表单验证
const isFormValid = computed(() => {
const hasUsername = form.value.username.trim().length > 0
const usernameValid = !usernameError.value
const hasPassword = isEditMode.value || form.value.password.length >= 6
// 编辑模式下如果填写了密码,必须确认密码一致
const passwordConfirmed = !isEditMode.value || form.value.password.length === 0 || form.value.password === form.value.confirmPassword
return hasUsername && hasPassword && passwordConfirmed
return hasUsername && usernameValid && hasPassword && passwordConfirmed
})
// 加载访问控制选项