golang 网络编程(8)表单

golang_real.jpg

其实本人现在有点讨厌搬砖的工作,但是有的时候还是的搬一搬。不搬那知道自己有力气。


th-14.jpeg

客户端代码
模板文件,下面是表单提交的客户端代码可能在熟悉不过了。就不多解释了。

 <form class="login_form" action="/login" method="POST">
 <div class="form_input">
 <label for="username">username</label>
 <input id="username" type="text" name="username">
 </div>
 <div class="form_input">
 <label for="password">password</label>
 <input id="password" type="password" name="password" />
 </div>
 <div class="form_submit">
 <input type="submit" value="login">
 </div>
 </form>

服务端代码

func login(w http.ResponseWriter, r *http.Request){
 fmt.Println("method: " + r.Method)
 r.ParseForm()
 if r.Method == "GET"{
 t, _ := template.ParseFiles("login.gtpl")
 t.Execute(w, nil)
 }else{
 fmt.Println("username: ", r.Form["username"])
 fmt.Println("password: ", r.Form["password"])
 }
}
  • 定义 login 的一个方法login的路由控制,当客户端发起 get 请求来访问 /login 服务端读取模板文件,返回给客户端一个登录界面就是上面的模板文件,用户完成用户名和密码填写后提交以 post 方式表单数据给服务端时候。客户端获取r.Form获取表单数据简单打印出来。这就完成一次客户端向服务端的表单提交
  • 注意要获取表单数据,客户端一定要先调用r.ParseForm()方法
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>Document</title>
 <style>
 body {
 display: flex;
 justify-content: center;
 align-items: center;
 background: lightgray;
 }
 .login_form {
 background: lightblue;
 padding: 12px;
 }
 .form_input {
 color: white;
 height: 24px;
 outline: none;
 border: none;
 }
 .form_submit input {
 color: dodgerblue;
 height: 24px;
 font-size: 18px;
 background: deepskyblue;
 }
 </style>
</head>
<body>
 <form class="login_form" action="/login" method="POST">
 <div class="form_input">
 <label for="username">username</label>
 <input id="username" type="text" name="username">
 </div>
 <div class="form_input">
 <label for="password">password</label>
 <input id="password" type="password" name="password" />
 </div>
 <div class="form_submit">
 <input type="submit" value="login">
 </div>
 </form>
</body>
</html>
package main
import(
 "fmt"
 "html/template"
 "log"
 "net/http"
 // "strings"
)
func index(w http.ResponseWriter, r *http.Request){
}
func login(w http.ResponseWriter, r *http.Request){
 fmt.Println("method: " + r.Method)
 r.ParseForm()
 if r.Method == "GET"{
 t, _ := template.ParseFiles("login.gtpl")
 t.Execute(w, nil)
 }else{
 fmt.Println("username: ", r.Form["username"])
 fmt.Println("password: ", r.Form["password"])
 }
}
func iconHandler(w http.ResponseWriter, r *http.Request) {
 
}
func main() {
 http.HandleFunc("/",index);
 http.HandleFunc("/login",login);
 server := &http.Server{
 Addr:":9090",
 }
 log.Println("Listening...")
 err := server.ListenAndServe()
 if err != nil{
 log.Fatal("Listen And Server ", err)
 }
}

在 web 应用开发中,我们经常会对邮件地址、用户名、电话号码以及居民身份中进行校验,下面列出了这些常用的正则表达式。

电子邮件地址
 if m, _ := regexp.MatchString(`([\w.\_]{2,10})@(\w{1,}).([a-z]{2,4})$`,"qq123@qq.com"); !m {
 fmt.Println("invalidated email address")
 }else{
 fmt.Println("validated")
 }
手机号码
 if m, _ := regexp.MatchString(`^(1[3|4|5|8][0-9]\d{4,8})$`,"13840008000"); !m {
 fmt.Println("invalidated phonenumber address")
 }else{
 fmt.Println("validated phonenumber")
 }
匹配中文
 if m, _ := regexp.MatchString("^[\\x{4e00}-\\x{{9fa5}}]+$","代码"); !m {
 fmt.Println("invalidated chinese")
 }else{
 fmt.Println("validated chinese")
 }

参看 go web 编程

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

相关阅读更多精彩内容

  • ORA-00001: 违反唯一约束条件 (.) 错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。 O...
    我想起个好名字 阅读 10,995评论 0赞 9
  • 本文为转载,原文:Golang Web学习(14)—— 表单处理 介绍 表单是我们平常编写Web应用常用的工具,通...
    ChainZhang 阅读 10,434评论 2赞 7
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun 阅读 28,401评论 1赞 45
  • 第二部分 Blog例子 第八章 用户验证 大部分程序需要追踪用户身份。当用户连接到程序,通过一系列步骤使自己的身份...
    易木成华 阅读 5,117评论 0赞 4
  • 最近状态很不好,所以就来分享一些我喜欢的句子吧! 如果有一天:你不再寻找爱情,只是去爱;你不再渴望成功,只是去做;...
    亚亚先生 阅读 1,369评论 2赞 1

友情链接更多精彩内容