This is a creation in
Article, where the information may have evolved or changed.
Go Language Form Verification
1. Required Fields
if len(r.Form["username"][0])==0{//为空的处理}
2. Digital Check
//the first method Getint,err:=strconv (R ( "age" ) if err!= nil {//Digital conversion error, then it may not be the number } //then you can determine the size range of the number if getint >100 { It's too big. }//second method "regular match" if m, _: = regexp< Span class= "hljs-variable". Matchstring ( "^[0-9]+$" , R (),!m {return false }
3. The Chinese check "Go" is RE2, all characters are UTF-8 encoded. 】
//对于中文我们目前有两种方式来验证,可以使用 unicode 包提供的//func Is(rangeTab *RangeTable, r rune) bool 来验证,也可以使//用正则方式来验证,这里使用最简单的正则方式if m, _ := regexp.MatchString("^\\p{Han}+$", r.Form.Get("realname")); !m { returnfalse}
4. English check
ifregexp.MatchString("^[a-zA-Z]+$", r.Form.Get("engname")); !m { false}
5. e-mail address
if m, _ := regexp.MatchString(`^([\w\.\_]{2,10})@(\w{1,}).([a-z]{2,4})$`, r.Form.Get("email")); !m { fmt.Println("no")}else{ fmt.Println("yes")}
6. Mobile phone number
if m, _ := regexp.MatchString(`^(1[3|4|5|8][0-9]\d{4,8})$`, r.Form.Get("mobile")); !m { returnfalse}
7. Drop-down menu
<select name="Fruit"><option value="Apple">apple</option><option value="Pear">pear</option><option value="Banane">banane</option></select>slice:=[]string{"Apple","Pear","Banane"} for_, V: = Range Slice {ifv = = R.form.get ("Fruit") {returntrue}}returnfalse
8. Radio button
type="radio" name="gender" value="1"type="radio" name="gender" value="2">女slice:=[]int{1,2}for_, v := range slice { if v == r.Form.Get("gender") { returntrue }}returnfalse
9. check box
<input Type="checkbox"Name="Interest" value="Football"> Football <input Type="checkbox"Name="Interest" value="Basketball"> Basketball <input Type="checkbox"Name="Interest" value="Tennis"> Tennis slice:=[]string{"Football","Basketball","Tennis"}a:=slice_diff (r.form["Interest"],slice)ifA = = nil{returntrue}returnfalse
10. Date and Time
//Go里面提供了一个timetime.Date(2009time1023000time.UTC)fmt.Printf("Go launched at %s\n", t.Local())//获取time之后我们就可以进行很多时间函数的操作。具体的判断就根据自己的需求调整。
11. Identification number
//verify 15-bit ID, 15-bit is all digits if m, _: = RegExp. Matchstring ( "^ (\d{15}) $ ' , R.form.get (" Usercard " ));!m {return false }//validates the 18-bit ID, the 18-bit first 17 digits are digits, the last one is the check digit, which can be either a number or a character X. if m, _: = RegExp. Matchstring ( "^ (\d{17}) ([0-9]| X) $ ' , R.form.get ( "Usercard" );!m {return false }