php开发技巧golang

golang 身份证号码处理

2020-06-22  本文已影响0人  顶尖少爷

身份证格式验证

valid:=validation.Validation{}
reg := regexp.MustCompile(`^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`)
valid.Match(this.GetString("identification_number"),reg,"identification_number").Message("identification_number format is fail")
var datas []string
if valid.HasErrors(){
        for _,err := range valid.Errors {
            datas = append(datas,err.Message)
        }
        this.Data["json"]= this.HTTP_ERROR_WITH_DATA(datas)
        this.ServeJSON()
        this.StopRun()
}

身份证获取年龄信息

func GetAgeWithIdentificationNumber(identification_number string)int{
    reg := regexp.MustCompile(`^[1-9]\d{5}(18|19|20)(\d{2})((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`)
    //reg := regexp.MustCompile(`^[1-9]\d{5}(18|19|20)`)
    params :=reg.FindStringSubmatch(identification_number)
    birYear,_:= strconv.Atoi(params[1]+params[2])
    birMonth,_ := strconv.Atoi(params[3])
    age :=time.Now().Year()-birYear
    if int(time.Now().Month()) <birMonth{
        age --
    }
    return age
}
上一篇 下一篇

猜你喜欢

热点阅读