Support chinese username.

This commit is contained in:
lishuang 2021-01-02 18:13:20 +08:00
parent 13b6d7f98d
commit 07ed4f7922
3 changed files with 37 additions and 2 deletions

View File

@ -23,7 +23,7 @@ const (
const (
//username pattern
USERNAME_PATTERN = `^[0-9a-z_]+$`
USERNAME_PATTERN = "^[\\p{Han}0-9a-zA-Z_]+$"
USERNAME_DEMO = "demo"
)

35
code/test/regex_test.go Normal file
View File

@ -0,0 +1,35 @@
package test
import (
"regexp"
"testing"
)
func TestUsername(t *testing.T) {
testMap := make(map[string]bool)
testMap[`tank`] = true
testMap[`孙悟空`] = true
testMap[`孙悟wukong`] = true
testMap[`孙悟八戒`] = true
testMap[`孙悟123`] = true
testMap[`西天123`] = true
testMap[`西天-123`] = false
testMap[`西天@123`] = false
testMap[`-西天@123`] = false
testMap[`hong hua`] = false
for k, v := range testMap {
pattern := "^[\\p{Han}0-9a-zA-Z_]+$"
usernameRegex := regexp.MustCompile(pattern)
//使用MatchString来将要匹配的字符串传到匹配规则中
result := usernameRegex.MatchString(k)
if v == result {
t.Logf(" %s = %v pass", k, v)
} else {
t.Errorf(" %s != %v error", k, v)
}
}
}

View File

@ -26,7 +26,7 @@ var (
UsernameExist = &Item{English: `username "%s" exists`, Chinese: `用户名"%s"已存在`}
UsernameNotExist = &Item{English: `username "%s" not exists`, Chinese: `用户名"%s"不存在`}
UsernameIsNotAdmin = &Item{English: `username "%s" is not admin user`, Chinese: `用户名"%s"不是管理员账号`}
UsernameError = &Item{English: `username can only be lowercase letters, numbers or _`, Chinese: `用户名必填,且只能包含小写字母,数字和'_'`}
UsernameError = &Item{English: `username can only be letters, numbers or _`, Chinese: `用户名必填,且只能包含中文,字母,数字和'_'`}
UserRegisterNotAllowd = &Item{English: `admin has banned register`, Chinese: `管理员已禁用自主注册`}
UserPasswordLengthError = &Item{English: `password at least 6 chars`, Chinese: `密码长度至少为6位`}
UserOldPasswordError = &Item{English: `old password error`, Chinese: `旧密码不正确`}