gpt4 book ai didi

C# DataAnnotations 正则表达式匹配不正确

转载 作者:行者123 更新时间:2023-11-30 22:53:04 24 4
gpt4 key购买 nike

我正在尝试使用 razor 页面验证用户以密码形式输入的内容。我有以下模型

    public class UserPassword
{
public Guid? Id{ get; set; }
public string Code { get; set; }
[StringLength(32, MinimumLength = 8, ErrorMessage = "The password must at least be 8 characters")]
[RegularExpression("[A-Z]", ErrorMessage = "The password Must contain atleast one uppercase character")]
public string Password { get; set; }
[Compare("Password")]
public string ConfirmedPassword { get; set; }
}

用生成的.cshtlm

@page "{id}/{code}"
@model AuthenticationService.Pages.UserPasswordModel

@{
ViewData["Title"] = "UserPassword";
}

<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="UserPassword.Password" class="control-label"></label>
<input asp-for="UserPassword.Password" class="form-control" />
<span asp-validation-for="UserPassword.Password" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="UserPassword.ConfirmedPassword" class="control-label"></label>
<input asp-for="UserPassword.ConfirmedPassword" class="form-control" />
<span asp-validation-for="UserPassword.ConfirmedPassword" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>

@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

此外,OnGet()OnPostAsync() 方法如下所示

 public IActionResult OnGet()
{
if (!ModelState.IsValid)
{
return Page();
}
return Page();
}

[BindProperty]
public UserPassword UserPassword { get; set; }

public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
UserPassword.Id = Guid.Parse(RouteData.Values["id"].ToString());
UserPassword.Code = Base64Utility.UrlSafeDecode(RouteData.Values["code"].ToString());
var user = await _userManager.FindByIdAsync(UserPassword.Id.ToString());
var result = await _userManager.ConfirmEmailAsync(user, UserPassword.Code);
if (result.Succeeded)
{
await _userManager.RemovePasswordAsync(user);
await _userManager.AddPasswordAsync(user, UserPassword.ConfirmedPassword);
return RedirectToPage("./Confirmation");
}
return RedirectToPage("./Error");
}

我的问题是数据注释正则表达式。正则表达式仅匹配输入表单的第一个字符,当向输入表单添加其他字符时,将显示 ErrorMessage“必须包含大写字符”,即使有多个大写字符作为如下图所示。 Example of <code>ErrorMessage</code>

最佳答案

对于工作模式,您可以尝试 "(?=.*[A-Z]).*"

对于可理解的模式,您可以尝试 ".*[A-Z]+.*" 这意味着:

  • .* 有任何字符
  • [A-Z]+有一个或多个大写。

关于C# DataAnnotations 正则表达式匹配不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57616547/

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