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 实现的基于web的文件管理-filebrowser

FileBrowser 在指定目录中提供了一个文件管理界面,可用于上传,删除,预览,重命名和编辑文件。它允许创建多个用户,每个用户都可以有自己的目录。它可以用作独立的应用程序。...

Golang quicktemplate 模版快速入门

Golang 有很多的模版引擎,自带的 `html/template` 也很好,大多数情况都能满足需求,只是有些逻辑、条件判断不好在模版里实现, `quicktemplate` 是个很好的选择。...

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

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