C# 使用RegularExpression注释验证模型
我在WebAPI项目中添加了一个过滤器,以验证从客户端传递的所有模型。为了验证模型,我使用了数据注释。除了我使用RegularExpression注释外,它似乎都可以正常工作 这是我在api中使用的过滤器:C# 使用RegularExpression注释验证模型,c#,.net,regex,asp.net-web-api,data-annotations,C#,.net,Regex,Asp.net Web Api,Data Annotations,我在WebAPI项目中添加了一个过滤器,以验证从客户端传递的所有模型。为了验证模型,我使用了数据注释。除了我使用RegularExpression注释外,它似乎都可以正常工作 这是我在api中使用的过滤器: public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext.ModelState.IsValid == false) {
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(
HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
这是未正确验证的模型属性:
[Required]
[MinLength(8)]
[StringLength(255)]
[RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)", ErrorMessage = "Password must contain at least one capital letter and one number")]
public string Password { get; set; }
无论我在字符串中传递什么值,我都会不断得到一个错误。如果有任何帮助,我们将不胜感激。试试这个:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).*$
Prob密码格式正确时,表达式返回null