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 实现 10 进制转 N 进制

给定一个不没有重复字符的字符串,如 `[0-9,a-z]`,把一个 10 进制数字转为,该字符集的字符串。应用场合如汽车牌、顺序计数。...

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

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