英文:
Regex Phone Number Using Validation V2 Golang Package Not Working
问题
我在使用github.com/go-validator/validator
验证正则表达式时遇到了一些问题,用于验证一些以+62、62、0为前缀的电话号码,例如+628112blabla、0822blablabla、628796blablabla。
我在在线正则表达式测试器上尝试了我的正则表达式,没有问题。这是我的正则表达式:
(0|\+62|062|62)[0-9]+$
但是当我尝试在Go中实现时,正则表达式不起作用。这是我用于实现目的的代码:
type ParamRequest struct {
PhoneNumber string `validate:"nonzero,regexp=(0|\+62|062|62)[0-9]+$"`
ItemCode string `validate:"nonzero"`
CallbackUrl string `validate:"nonzero"`
}
func (c *TopupAlloperatorApiController) Post() {
var v models.TopupAlloperatorApi
interf := make(map[string]interface{})
json.Unmarshal(c.Ctx.Input.RequestBody, &interf)
logs.Debug(" Json Input Request ", interf)
var phone, item, callback string
if _, a := interf["PhoneNumber"].(string); a {
phone = interf["PhoneNumber"].(string)
}
if _, b := interf["ItemCode"].(string); b {
item = interf["ItemCode"].(string)
}
if _, c := interf["CallbackUrl"].(string); c {
callback = interf["CallbackUrl"].(string)
}
ve := ParamRequest{
PhoneNumber: phone,
ItemCode: item,
CallbackUrl: callback,
}
logs.Debug(" Param Request ", ve)
err := validator.Validate(ve)
if err == nil {
//success
}else{
// not success
}
非常感谢您的帮助。谢谢。
英文:
I am having some trouble when using github.com/go-validator/validator
to validate regex some phone numbers with this prefix +62, 62, 0, for instance number e.g. +628112blabla, 0822blablabla, 628796blablabla.
I have try my regex on online regex tester and no issue with the regex on that. Here the regex is :
(0|\+62|062|62)[0-9]+$
But when I try with my go implement with it, the regex not working. This is my code for implement the purpose :
type ParamRequest struct {
PhoneNumber string `validate:"nonzero,regexp=(0|\+62|062|62)[0-9]+$"`
ItemCode string `validate:"nonzero"`
CallbackUrl string `validate:"nonzero"`
}
func (c *TopupAlloperatorApiController) Post() {
var v models.TopupAlloperatorApi
interf := make(map[string]interface{})
json.Unmarshal(c.Ctx.Input.RequestBody, &interf)
logs.Debug(" Json Input Request ", interf)
var phone, item, callback string
if _, a := interf["PhoneNumber"].(string); a {
phone = interf["PhoneNumber"].(string)
}
if _, b := interf["ItemCode"].(string); b {
item = interf["ItemCode"].(string)
}
if _, c := interf["CallbackUrl"].(string); c {
callback = interf["CallbackUrl"].(string)
}
ve := ParamRequest{
PhoneNumber: phone,
ItemCode: item,
CallbackUrl: callback,
}
logs.Debug(" Param Request ", ve)
err := validator.Validate(ve)
if err == nil {
//success
}else{
// not success
}
Many thanks for anything help. Thank you.
答案1
得分: 1
因为您正在使用正则表达式来检查PhoneNumber
,如果值为空,则不会匹配,所以最好将验证中的nonzero
移除。
我查阅了文档,没有找到可以同时使用nonzero
和regexp
的示例。
此外,您需要对正则表达式进行符号转义,否则它将无法被反射检测到。这意味着您应该在代码中使用(0|\\+62|062|62)[0-9]+$
。这里有一个示例,问题就在于符号转义在结构标签中的使用。
另外,请尝试使用这个正则表达式:^\\+{0,1}0{0,1}62[0-9]+$
。
英文:
Because you are using regexp to check PhoneNumber
that won't be matching if the value is empty it is better to remove nonzero
from the validation.
I have checked out documentation and haven't found examples where you can use both: nonzero
and regexp
.
Also you need to make your regex symbol-escaped, otherwise it won't be detected by reflection. It means you should use (0|\\+62|062|62)[0-9]+$
in your code. Here is example where problem is: symbol escaping in struct tags
And also, please try to use this regexp: ^\\+{0,1}0{0,1}62[0-9]+$
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论