Go不识别正则表达式。

huangapple go评论72阅读模式
英文:

Go doesn't recognize regexp

问题

我需要将特殊字符(@#$%^& .?)添加到oneof或使用regexp,但是regexp在执行时出现错误是未定义的。我目前正在使用oneof=大写&小写&数字。此验证器将检查密码是否至少包含1个大写字母、1个小写字母、1个数字和1个特殊字符。

示例代码:

package main

import (
	"fmt"

	"github.com/go-playground/validator/v10"
)

type UserReg struct {
	Username        string `validate:"required,min=4,max=15"`
	Email           string `validate:"required,email"`
	Password        string `validate:"required,min=8,max=20,eqfield=ConfirmPassword,oneof=uppercase&lowercase&numeric"`
	ConfirmPassword string `validate:"required,min=8,max=20,oneof=uppercase&lowercase&numeric"`
}

type UserReg2 struct {
	Username        string `validate:"required,min=4,max=15"`
	Email           string `validate:"required,email"`
	Password        string `validate:"required,min=8,max=20,eqfield=ConfirmPassword,regexp=^(?=.*[a-zA-Z0-9!@#$%^&.?/]).$"`
	ConfirmPassword string `validate:"required,min=8,max=20,regexp=^(?=.*[a-zA-Z0-9!@#$%^&.?/]).$"`
}

func main() {

	fmt.Println("开始工作测试,不检查特殊字符")

	ut := UserReg{
		Username:        "Joe23",
		Email:           "Joe@joe.com",
		Password:        "Pa55w0rd",
		ConfirmPassword: "Pa55w0rd",
	}

	validate := validator.New()

	err := validate.Struct(ut)
	fmt.Println(err)

	fmt.Println("结束工作测试,不检查特殊字符")

	fmt.Println("开始失败测试,检查特殊字符")

	ut2 := UserReg2{
		Username:        "Joe23",
		Email:           "Joe@joe.com",
		Password:        "Pa$$w0rd",
		ConfirmPassword: "Pa$$w0rd",
	}

	err2 := validate.Struct(ut2)
	fmt.Println(err2)

	fmt.Println("结束失败测试,检查特殊字符")
}
英文:

I need to add special characters (@#$%^&.?) to the oneof or use regexp, but regexp errors on execution is undefined. I am currently using, oneof=uppercase&lowercase&numeric. this validator would be checking if the passwords contain at least 1 uppercase letter, 1 lowercase letter, 1 number and 1 special character.

Sample Code ==>

package main

import (
	"fmt"

	"github.com/go-playground/validator/v10"
)

type UserReg struct {
	Username        string `validate:"required,min=4,max=15"`
	Email           string `validate:"required,email"`
	Password        string `validate:"required,min=8,max=20,eqfield=ConfirmPassword,oneof=uppercase&lowercase&numeric"`
	ConfirmPassword string `validate:"required,min=8,max=20,oneof=uppercase&lowercase&numeric"`
}

type UserReg2 struct {
	Username        string `validate:"required,min=4,max=15"`
	Email           string `validate:"required,email"`
	Password        string `validate:"required,min=8,max=20,eqfield=ConfirmPassword,regexp=^(?=.*[a-zA-Z0-9!@#$%^&.?/]).$"`
	ConfirmPassword string `validate:"required,min=8,max=20,regexp=^(?=.*[a-zA-Z0-9!@#$%^&.?/]).$"`
}

func main() {

	fmt.Println("Start of working test w/o check for special characters")

	ut := UserReg{
		Username:        "Joe23",
		Email:           "Joe@joe.com",
		Password:        "Pa55w0rd",
		ConfirmPassword: "Pa55w0rd",
	}

	validate := validator.New()

	err := validate.Struct(ut)
	fmt.Println(err)

	fmt.Println("End of working test w/o check for special characters")

	fmt.Println("Start of failing test w/ check for special characters")

	ut2 := UserReg2{
		Username:        "Joe23",
		Email:           "Joe@joe.com",
		Password:        "Pa$$w0rd",
		ConfirmPassword: "Pa$$w0rd",
	}

	err2 := validate.Struct(ut2)
	fmt.Println(err2)

	fmt.Println("End of failing test w/ check for special characters")
}

答案1

得分: 2

在玩弄了一番之后,我通过更改UserReg中的代码行得出了以下结果:

	Password        string `validate:"required,min=8,max=20,eqfield=ConfirmPassword,oneof=uppercase&lowercase&numeric&@ # $ % ^ & . ? /"`
	ConfirmPassword string `validate:"required,min=8,max=20,oneof=uppercase&lowercase&numeric&@ # $ % ^ & . ? /"`
英文:

After playing around I came up with by changing the line in UserReg to:

	Password        string `validate:"required,min=8,max=20,eqfield=ConfirmPassword,oneof=uppercase&lowercase&numeric&@ # $ % ^ \& . ? \/"`
	ConfirmPassword string `validate:"required,min=8,max=20,oneof=uppercase&lowercase&numeric&@ # $ % ^ \& . ? \/"`

huangapple
  • 本文由 发表于 2022年12月24日 01:29:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/74902409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定