Skip to content

Commit 0d1fe2d

Browse files
authored
Merge pull request #400 from dragove/feat/valid-username
feat: 用户名校验添加前后不能包含空格校验
2 parents 0b38211 + b85f482 commit 0d1fe2d

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/components/account/AuthFormShared.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from 'react-hook-form'
88

99
import { FormField, FormFieldProps } from 'components/FormField'
10-
import { REGEX_EMAIL } from 'utils/regexes'
10+
import { REGEX_EMAIL, REGEX_USERNAME } from 'utils/regexes'
1111

1212
export type RuleKeys = 'email' | 'password' | 'username' | 'registertoken'
1313

@@ -25,6 +25,7 @@ export const rule: Record<RuleKeys, UseControllerProps['rules']> = {
2525
required: '用户名为必填项',
2626
minLength: { value: 4, message: '用户名长度不能小于 4 位' },
2727
maxLength: { value: 24, message: '用户名长度不能大于 24 位' },
28+
pattern: { value: REGEX_USERNAME, message: '用户名前后不能包含空格' },
2829
},
2930
registertoken: {
3031
required: '邮箱验证码为必填项',

src/utils/regexes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
// let the server do the validation further and, if an email cannot be sent,
33
// a sufficient error message is far better than tied-to-RFC validation on client side.
44
export const REGEX_EMAIL = /^\S+@\S+\.\S+$/
5+
// 开头和结尾不能为空格,但是中间允许有空格
6+
export const REGEX_USERNAME = /^\S.{2,22}\S$/

0 commit comments

Comments
 (0)