我向我的web项目添加了一个过滤器,以验证从客户端传递的所有模型。为了验证模型,我使用数据注释。这一切似乎都很好,除非我使用了RegularExpression注释。
这是我的api中的过滤器:
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; }
不管我在字符串中传递了什么值,我都会得到一个错误。在这方面的任何帮助都将不胜感激。
发布于 2014-10-28 22:49:04
试试这个:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).*$
提示当密码格式良好时,表达式返回null
。
https://stackoverflow.com/questions/26619778
复制相似问题