gpt4 book ai didi

regex - 如何使用 FluentValidation 创建强密码?

转载 作者:行者123 更新时间:2023-12-02 02:39:18 25 4
gpt4 key购买 nike

我正在使用 FluentValidation 设计用户注册屏幕。

我想建立一个控制机制,提供以下所有步骤的信息。

我试过的代码;

RuleFor(p => p.Password).Matches(@"[A-Z]+").WithMessage("Your password must contain at least one uppercase letter.");
RuleFor(p => p.Password).Matches(@"[a-z]+").WithMessage("Your password must contain at least one lowercase letter.");
RuleFor(p => p.Password).Matches(@"[0-9]+").WithMessage("Your password must contain at least one number.");
RuleFor(x => x.Password).Matches(@"[\!\?\*\.]*$").WithMessage("Your password must contain at least one (!? *.).");

但是我无法达到我想要的结果。我还查看了 FluentValidation 文档,但没有看到任何有用的示例。

如果你能帮上忙,我会很高兴。

谢谢。

最佳答案

请引用下面的例子:

public class Login
{
public string Password { get; set; }
}


public class PassWordValidator : AbstractValidator<Login>
{
public PassWordValidator()
{
RuleFor(p => p.Password).NotEmpty().WithMessage("Your password cannot be empty")
.MinimumLength(8).WithMessage("Your password length must be at least 8.")
.MaximumLength(16).WithMessage("Your password length must not exceed 16.")
.Matches(@"[A-Z]+").WithMessage("Your password must contain at least one uppercase letter.")
.Matches(@"[a-z]+").WithMessage("Your password must contain at least one lowercase letter.")
.Matches(@"[0-9]+").WithMessage("Your password must contain at least one number.")
.Matches(@"[\!\?\*\.]+").WithMessage("Your password must contain at least one (!? *.).");
}
}

行动:

 [HttpPost]
public IActionResult TestPassword(Login model)
{
PassWordValidator _validator = new PassWordValidator();
var validResult = _validator.Validate(model);
if (!validResult.IsValid)
{
return BadRequest(validResult.Errors);
}
return Ok();
}

这是 postman 的测试结果:

enter image description here

关于regex - 如何使用 FluentValidation 创建强密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63864594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com