douzong5473 2017-07-26 10:35
浏览 124
已采纳

使用验证V2 Golang软件包的正则表达式电话号码不起作用

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条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 在matlab中如何进行三个参数的离散傅里叶逆变换(idft)
      • ¥15 遇到问题了,求解答!
      • ¥15 请问coppliasim eduUR5视觉抓取怎么实现仿真,
      • ¥30 JavaWeb实验(购物平台)
      • ¥15 八路抢答器倒计时设计时显示器不输出,只能显示0
      • ¥15 用C语言随机生成一个迷宫
      • ¥15 超多因素的正交方案设计
      • ¥15 Scratch~汽车小游戏
      • ¥30 OSGB转换为3dtiles
      • ¥25 用于Audio的芯片中“Audio Interface”和“Mode Control”是什么?