美文网首页Golang程序员Go
golang 网络编程(8)表单

golang 网络编程(8)表单

作者: zidea | 来源:发表于2019-04-25 14:45 被阅读28次
    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 编程

    相关文章

      网友评论

        本文标题:golang 网络编程(8)表单

        本文链接:https://www.haomeiwen.com/subject/tdpwgqtx.html