首页 > 解决方案 > 在 C# 中具有流畅验证的正则表达式 - 如何在密码中不允许空格和某些特殊字符?

问题描述

到目前为止,这是我的 C# 应用程序中对密码的流畅验证

RuleFor(request => request.Password)
    .NotEmpty()
    .MinimumLength(8)
    .Matches("[A-Z]+").WithMessage("'{PropertyName}' must contain one or more capital letters.")
    .Matches("[a-z]+").WithMessage("'{PropertyName}' must contain one or more lowercase letters.")
    .Matches(@"(\d)+").WithMessage("'{PropertyName}' must contain one or more digits.")
    .Matches(@"[""!@$%^&*(){}:;<>,.?/+\-_=|'[\]~\\]").WithMessage("'{ PropertyName}' must contain one or more special characters.")
    .Matches("(?!.*[£# “”])").WithMessage("'{PropertyName}' must not contain the following characters £ # “” or spaces.")
    .Must(pass => !blacklistedWords.Any(word => pass.IndexOf(word, StringComparison.OrdinalIgnoreCase) >= 0))
        .WithMessage("'{PropertyName}' contains a word that is not allowed.");

以下部分目前不起作用

.Matches("(?!.*[£# “”])").WithMessage("'{PropertyName}' must not contain the following characters £ # “” or spaces.")

例如,当密码为“Hello12!#”时,不会返回验证错误。£ # “” 和空格不应出现在密码中,如果其中任何一个存在,验证将失败,并且“{PropertyName}”不得包含以下字符 £ # “”或空格。错误信息。

如何修改它以使其正常工作?

标签: c#regexvalidationfluentvalidation

解决方案



推荐阅读