golang 身份证号码处理

身份证格式验证

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
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容