上一节,咱们简单说了如何获取表单的信息,现在简单说一下验证表单的输入.
GO文件
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"regexp"
"strconv"
"strings"
)
func saygo(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
t, _ := template.ParseFiles("form.html")
t.Execute(w, nil)
} else {
w.Header().Set("Content-Type", "text/html;charset=utf-8")
r.ParseForm()
//必填判断
fmt.Fprintf(w, r.Form["uid"][0]+"<br/>")
if len(r.Form["uid"][0]) == 0 {
fmt.Fprintf(w, "必填不能为空"+"<br/>")
}
//数字判断
fmt.Fprintf(w, r.Form.Get("age")+"<br/>")
getint, err := strconv.Atoi(r.Form.Get("age"))
if err != nil {
fmt.Fprintf(w, "不是数字"+"<br/>")
} else if getint > 100 {
fmt.Fprintf(w, "数字不能大于100"+"<br/>")
}
//电子邮件
fmt.Fprintf(w, r.Form.Get("email")+"<br/>")
if m, _ := regexp.MatchString(`^([\w\.\_]{2,10})@(\w{1,}).([a-z]{2,4})$`, r.Form.Get("email")); !m {
fmt.Fprintf(w, "不是合法的邮件"+"<br/>")
}
//下拉菜单
fmt.Fprintf(w, r.Form.Get("area")+"<br/>")
slice := []string{"1111", "2222", "333"}
is_slice := false
for _, v := range slice {
if v == r.Form.Get("area") {
is_slice = true
}
}
if is_slice != true {
fmt.Fprintf(w, "不是合法的下拉菜单"+"<br/>")
}
//单选按钮
fmt.Fprintf(w, r.Form.Get("gender")+"<br/>")
slice2 := []int{1, 2}
is_slice2 := false
for _, v := range slice2 {
if strconv.Itoa(v) == r.Form.Get("gender") {
is_slice2 = true
}
}
if is_slice2 != true {
fmt.Fprintf(w, "不是合法的单选"+"<br/>")
}
//复选框
slice3 := r.Form["interest"]
fmt.Fprintf(w, strings.Join(slice3, ",")+"<br/>")
m := make(map[string]int)
m["football"] = 1
m["basketball"] = 1
m["tennis"] = 1
is_slice3 := true
for _, v := range slice3 {
if int(m[v]) <= 0 {
is_slice3 = false
}
}
if is_slice3 != true {
fmt.Fprintf(w, "不是合法的复选"+"<br/>")
}
fmt.Fprintf(w, "<br/>")
t, _ := template.ParseFiles("form.html")
t.Execute(w, nil)
}
}
func main() {
http.HandleFunc("/", saygo)
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
模板文件:
<html>
<head>
<title>form示例</title>
</head>
<body>
<form action="" method="post">
必填:<input type="text" name="uid"><br/>
数字:<input type="text" name="age"><br/>
电子邮件:<input type="text" name="email"><br/>
下拉菜单:<select name="area">
<option value="0000">0000</option>
<option value="1111">1111</option>
<option value="2222">2222</option>
<option value="3333">3333</option>
</select><br/>
单选按钮:<input type="radio" name="gender" value="1">男
<input type="radio" name="gender" value="2">女<br/>
复选框:<input type="checkbox" name="interest" value="football">足球
<input type="checkbox" name="interest" value="basketball">篮球
<input type="checkbox" name="interest" value="tennis">网球
<input type="checkbox" name="interest" value="0000">不合法
<input type="submit" value="提交">
</form>
</body>
</html>
这里只是简单说明,哈哈!
Those people shouldn’t be allowed to do that
gay porn Oscars 2010 Red Carpet Best and Worst Dressed photos
I will not stop wearing the high end tie dye clothing
how to lose weight fastJoico K Pack Reconstructor Conditioner best High End for dry
how to lose weight fastJoico K Pack Reconstructor Conditioner best High End for dry
