Golang程序员Go

golang 网络编程(8)表单

2019-04-25  本文已影响28人  zidea
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"])
    }
}
<!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 编程

上一篇下一篇

猜你喜欢

热点阅读