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 Range 的性能提升Tip

Go 语言里使用 range 可以方便遍历数组(array)、切片(slice)、字典(map)和信道(chan)。这里主要关注他们的性能。...

Golang 把cookie 字符串解析为cookie 结构

在做爬虫时有时候会遇到需要带已登录的 cookie 请求,这个时候最简单的方法是在浏览器登录后,在开发者面板找到cookie 字符串,然后拷贝粘贴。这就面临一个问题需要把cookie 字符串解析成Go 语言 cookie 结构体。...

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

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