go - Regex Phone Number Using Validation V2 Golang Package Not Working -
i having trouble when using github.com/go-validator/validator validate regex phone numbers prefix +62, 62, 0, instance number e.g. +628112blabla, 0822blablabla, 628796blablabla.
i have try regex on online regex tester , no issue regex on that. here regex :
(0|\+62|062|62)[0-9]+$ but when try go implement it, regex not working. code implement 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 _, := interf["phonenumber"].(string); { 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 help. thank you.
because using regexp check phonenumber won't matching if value empty better remove nonzero validation.
have checked out documentation , haven't found examples can use both: nonzero , regexp.
need make regex symbol-escaped, otherwise won't detected reflection. means should use (0|\\+62|062|62)[0-9]+$ in code. here example problem is: symbol escaping in struct tags
, also, please try use regexp: ^\\+{0,1}0{0,1}62[0-9]+$
Comments
Post a Comment