golang 验证表单的输入
login.gtpl 文件的内容:
<html>
<head><title></title>
</head>
<body>
<form action="/login" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登陆"><br>
<hr>
Age:<input type="text" name="age"><br>
Real Name:<input type="text" name="realname"><br>
English Name:<input type="text" name="engname"><br>
Email:<input type="text" name="email"><br>
Mobile:<input type="text" name="mobile"><br>
<select name="fruit">
<option value="apple">apple</option>
<option value="pear">pear</option>
<option value="banana">banana</option>
</select><br>
<input type="radio" name="gender" value="1">男 <br>
<input type="radio" name="gender" value="2">女 <br>
<input type="checkbox" name="interest" value="football">足球 <br>
<input type="checkbox" name="interest" value="basketball">篮球 <br>
<input type="checkbox" name="interest" value="tennis">网球 <br>
日期时间:<input type="text" name="txtDateTime"><br>
身份证(15位):<input type="text" name="id1card"><br>
身份证(18位):<input type="text" name="id2card"><br>
</form>
</body>
</html>
main.go 文件的内容(它是个web服务器):
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
type MyMux struct {
}
func (p *MyMux) ServeHTTP(res http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/" {
SayHi(res, req)
return
} else if req.URL.Path == "/login" {
Login(res, req)
return
}
http.NotFound(res, req)
return
}
func SayHi(res http.ResponseWriter, req *http.Request) {
fmt.Fprint(res, "Hello World")
}
func Login(res http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
t, _ := template.ParseFiles("login.gtpl")
log.Println(t.Execute(res, nil))
//fmt.Fprint(res, req.Form["username"], req.Form["password"])
} else {
if len(req.FormValue("username")) == 0 {
fmt.Fprint(res, "用户名不能为空")
fmt.Fprint(res, "\npassword: ", req.FormValue("password"))
return
}
// if m, _ := regexp.MatchString("^[0-9]+$", req.Form.Get("age")); !m { return false }
age, err := strconv.Atoi(req.Form.Get("age"))
if err != nil {
fmt.Fprint(res, "年龄请输入数值")
return
}
if age > 140 || age < 0 {
fmt.Fprint(res, "年龄取值范围在0 ~ 140岁之间")
return
}
if m, _ := regexp.MatchString("^\\p{Han}+$", req.Form.Get("realname")); !m {
fmt.Fprint(res, "真实的姓名不是汉字")
return
}
if m, _ := regexp.MatchString("^[a-zA-Z]+$", req.Form.Get("engname")); !m {
fmt.Fprint(res, "英文名字输入的不是英文")
return
}
if m, _ := regexp.MatchString(`^([\w\.\_]{2,10})@(\w{1,}).([a-z]{2,4})$`, req.Form.Get("email")); !m {
fmt.Fprint(res, "电子邮件格式不正确")
return
}
if m, _ := regexp.MatchString(`^(1[3|4|5|8][0-9]\d{4,8})$`, req.Form.Get("mobile")); !m {
fmt.Fprint(res, "您输入的不是手机号码")
return
}
slice1 := []string{"apple", "pear", "banane"}
found1 := false
v1 := req.Form.Get("fruit")
for _, item := range slice1 {
if item == v1 {
found1 = true
break
}
}
if found1 == false {
fmt.Fprint(res, "系统发现您在伪造下拉菜单中不存在的选项")
return
}
slice2 := []int{1, 2}
found2 := false
for _, v2 := range slice2 {
gender, err := strconv.Atoi(req.Form.Get("gender"))
if err == nil && v2 == gender {
found2 = true
break
}
}
if found2 == false {
fmt.Fprint(res, "系统发现您在伪造选项中不存在的选项")
return
}
m := map[string]int{"football": 0, "basketball": 1, "tennis": 2}
found3 := false
for _, sport := range req.Form["interest"] {
_, exist := m[sport]
if exist == false {
found3 = true
break
}
}
if found3 == true {
fmt.Fprint(res, "系统发现您在伪造复选框选项")
return
}
mytime, err := String2Time(req.Form.Get("txtDateTime"))
if err != nil {
fmt.Fprint(res, "您输入的不是日期时间格式的数据")
return
}
//验证15位身份证,15位的是全部数字
if m, _ := regexp.MatchString(`^(\d{15})$`, req.Form.Get("id1card")); !m {
fmt.Fprint(res, "您输入的不是15位身份证号码")
return
}
//验证18位身份证,18位前17位为数字,最后一位是校验位,可能为数字或字符X
if m, _ := regexp.MatchString(`^(\d{17})([0-9]|X)$`, req.Form.Get("id2card")); !m {
fmt.Fprint(res, "您输入的不是18位身份证号码")
return
}
//请求的是登陆数据,那么执行登陆的逻辑判断
req.ParseForm() // req.FormValue("username") 调用req.FormValue时会自动调用req.ParseForm,所以不必提前调用
fmt.Println("username:", req.Form["username"]) //必需提前调用req.ParseForm()
fmt.Println("password:", req.Form["password"])
fmt.Fprint(res, "\n username:", req.Form["username"][0], " ", "password:", req.Form["password"][0])
fmt.Fprint(res, "\n age:", age)
fmt.Fprint(res, "\n Real Name:", req.Form.Get("realname"))
fmt.Fprint(res, "\n English Name:", req.Form.Get("engname"))
fmt.Fprint(res, "\n Email:", req.Form.Get("email"))
fmt.Fprint(res, "\n Mobile:", req.Form.Get("mobile"))
fmt.Fprint(res, "\n Fruit:", req.Form.Get("fruit"))
fmt.Fprint(res, "\n Gender:", req.Form.Get("gender"))
fmt.Fprint(res, "\n Interests:", req.Form["interest"])
fmt.Fprint(res, "\n txtDateTime: ", mytime)
fmt.Fprint(res, "\n 15位身份证: ", req.Form.Get("id1card"))
fmt.Fprint(res, "\n 18位身份证: ", req.Form.Get("id2card"))
}
}
func main() {
mux := &MyMux{}
http.ListenAndServe(":8080", mux) // http://127.0.0.1:8080/login
}
func String2Time(in string) (out time.Time, err error) {
in = strings.Replace(in, "/", "-", -1)
fmt.Println(in)
if len(in) > 10 {
out, err = time.Parse("2006-01-02 15:04:05", in) //layout使用"2006/01/02 15:04:05"此数据格式转换会出错
} else {
out, err = time.Parse("2006-01-02", in) //layout使用"2006/01/02"此数据格式转换会出错
}
return out, err
}