首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >go实现表单验证

go实现表单验证

作者头像
公众号-利志分享
发布于 2022-04-25 08:21:55
发布于 2022-04-25 08:21:55
1.6K0
举报
文章被收录于专栏:利志分享利志分享

package main import ( "fmt" "html/template" "log" "net/http" "regexp" "strconv" ) func sayHelloName(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "hello box") } func login(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { t, _ := template.ParseFiles("login.html") t.Execute(w, nil) } else if r.Method == "POST" { username := r.FormValue("username") password := r.FormValue("password") phone := r.FormValue("phone") like := r.FormValue("like") sex := r.FormValue("sex") utype := r.FormValue("utype") fmt.Println(like) fmt.Println(sex) fmt.Println(utype) //获取年龄之后转成int型 age, err := strconv.Atoi(r.FormValue("age")) if err != nil { w.Write([]byte("数字转化出错了,那么可能就不是数字")) return } if username == "" || password == "" || age == 0 { w.Write([]byte("username and password and age must not null")) return } //获取数据判定大小 if age > 100 { w.Write([]byte("age is to big")) return } if m, _ := regexp.MatchString(`^(1[3|4|5|8][0-9]\d{4,8})$`, phone); !m { w.Write([]byte("phone is error")) return } } else { fmt.Println("error") } } func main() { http.HandleFunc("/", sayHelloName) http.HandleFunc("/login", login) err := http.ListenAndServe(":8081", nil) if err != nil { log.Fatal("ListenAndServe:", err) } }

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-09-18,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档