diff --git a/util/gvalid/gvalid_check.go b/util/gvalid/gvalid_check.go index be420b392..0db27f0df 100644 --- a/util/gvalid/gvalid_check.go +++ b/util/gvalid/gvalid_check.go @@ -58,6 +58,7 @@ var ( "date-format": {}, "email": {}, "phone": {}, + "phone-loose": {}, "telephone": {}, "passport": {}, "password": {}, @@ -356,6 +357,7 @@ func doCheckBuildInRules( // 1. China Mobile: // 134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 183, 184, 187, 188, // 178(4G), 147(Net); + // 172 // // 2. China Unicom: // 130, 131, 132, 155, 156, 185, 186 ,176(4G), 145(Net), 175 @@ -372,7 +374,12 @@ func doCheckBuildInRules( // 6. 2018: // 16x, 19x case "phone": - match = gregex.IsMatchString(`^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^16[\d]{9}$|^17[0,3,5,6,7,8]{1}\d{8}$|^18[\d]{9}$|^19[\d]{9}$`, valueStr) + match = gregex.IsMatchString(`^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^16[\d]{9}$|^17[0,2,3,5,6,7,8]{1}\d{8}$|^18[\d]{9}$|^19[\d]{9}$`, valueStr) + // Loose mobile phone number verification(宽松的手机号验证) + // As long as the 11 digit numbers beginning with + // 13, 14, 15, 16, 17, 18, 19 can pass the verification (只要满足 13、14、15、16、17、18、19开头的11位数字都可以通过验证) + case "phone-loose": + match = gregex.IsMatchString(`^1(3|4|5|6|7|8|9)\d{9}$`, valueStr) // Telephone number: // "XXXX-XXXXXXX" diff --git a/util/gvalid/gvalid_unit_basic_all_test.go b/util/gvalid/gvalid_unit_basic_all_test.go index f147aea06..2b62d2782 100755 --- a/util/gvalid/gvalid_unit_basic_all_test.go +++ b/util/gvalid/gvalid_unit_basic_all_test.go @@ -237,6 +237,22 @@ func Test_Phone(t *testing.T) { }) } +func Test_PhoneLoose(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + err1 := gvalid.Check("13333333333", "phone-loose", nil) + err2 := gvalid.Check("15555555555", "phone-loose", nil) + err3 := gvalid.Check("16666666666", "phone-loose", nil) + err4 := gvalid.Check("23333333333", "phone-loose", nil) + err5 := gvalid.Check("1333333333", "phone-loose", nil) + err6 := gvalid.Check("10333333333", "phone-loose", nil) + t.Assert(err1, nil) + t.Assert(err2, nil) + t.Assert(err3, nil) + t.AssertNE(err4, nil) + t.AssertNE(err5, nil) + t.AssertNE(err6, nil) + }) +} func Test_Telephone(t *testing.T) { gtest.C(t, func(t *gtest.T) { rule := "telephone" diff --git a/util/gvalid/i18n/cn/validation.toml b/util/gvalid/i18n/cn/validation.toml index 7ed7501a0..fab6dfba6 100644 --- a/util/gvalid/i18n/cn/validation.toml +++ b/util/gvalid/i18n/cn/validation.toml @@ -9,6 +9,7 @@ "gf.gvalid.rule.date-format" = ":attribute 日期格式不满足:format" "gf.gvalid.rule.email" = ":attribute 邮箱地址格式不正确" "gf.gvalid.rule.phone" = ":attribute 手机号码格式不正确" +"gf.gvalid.rule.phone-loose" = ":attribute 手机号码格式不正确" "gf.gvalid.rule.telephone" = ":attribute 电话号码格式不正确" "gf.gvalid.rule.passport" = ":attribute 账号格式不合法,必需以字母开头,只能包含字母、数字和下划线,长度在6~18之间" "gf.gvalid.rule.password" = ":attribute 密码格式不合法,密码格式为任意6-18位的可见字符" diff --git a/util/gvalid/i18n/en/validation.toml b/util/gvalid/i18n/en/validation.toml index f6cb071da..7dc56cab0 100644 --- a/util/gvalid/i18n/en/validation.toml +++ b/util/gvalid/i18n/en/validation.toml @@ -9,6 +9,7 @@ "gf.gvalid.rule.date-format" = "The :attribute value does not match the format :format" "gf.gvalid.rule.email" = "The :attribute value must be a valid email address" "gf.gvalid.rule.phone" = "The :attribute value must be a valid phone number" +"gf.gvalid.rule.phone-loose" = "The :attribute value must be a valid phone number" "gf.gvalid.rule.telephone" = "The :attribute value must be a valid telephone number" "gf.gvalid.rule.passport" = "The :attribute value is not a valid passport format" "gf.gvalid.rule.password" = "The :attribute value is not a valid passport format"