用golang 标准库regexp 验证用户名和密码的合法性,主要是限制字符集和长度。
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 转摘请注明来源