GolangNote

Golang笔记

用 golang 标准库regexp 验证用户名和密码的合法性

Permalink

用golang 标准库regexp 验证用户名和密码的合法性,主要是限制字符集和长度。

golang regexp

Go: golang 正则检测用户名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main

import (
    "fmt"
    "regexp"
)

func main() {
    fmt.Println(CheckPassword("aa"))    // false
    fmt.Println(CheckUsername("admin")) // true
}

func CheckPassword(password string) (b bool) {
    if ok, _ := regexp.MatchString("^[a-zA-Z0-9]{4,16}$", password); !ok {
        return false
    }
    return true
}

func CheckUsername(username string) (b bool) {
    if ok, _ := regexp.MatchString("^[a-zA-Z0-9]{4,16}$", username); !ok {
        return false
    }
    return true
}

本文网址: https://golangnote.com/topic/4.html 转摘请注明来源

Related articles

Golang phantomjs 动态代理实现

phantomjs 是个很优秀的软件,虽然现在被chrome headless 抢了风头,但在某些特定场合,使用phantomjs 还是很方便,这里是介绍使用Go 实现动态代理。...

Golang Web 程序生产环境独立部署示例

一个 web 应用通常是跑在一个前端代理,如 Nginx 后,这样可以方便的在同一个服务器部署多个应用。这里说的独立部署是指让 go web 程序直接暴露在外面,独占 443、80 端口(俗称裸跑)。这样做除了性能有些提高外,更重要的是部署方便。...

Write a Comment to "用 golang 标准库regexp 验证用户名和密码的合法性"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 0ms