相关疑难解决方法(0)

使用regex c#验证密码

这一定很简单,我希望它以同样的方式工作,但它并没有帮助我.

using System;
using System.Text.RegularExpressions;
Run Code Online (Sandbox Code Playgroud)

我需要在password validation regular expression某些条件下 -

1)必须至少包含一个数字

2)一个大写字母

3)长8个字符.

public class Program
{
     public static bool IsValidPassword(string plainText) {
            Regex regex = new Regex(@"^(.{0,7}|[^0-9]*|[^A-Z])$");
            Match match = regex.Match(plainText);
            return match.Success;
}
    public static void Main()
    {
        Console.WriteLine(IsValidPassword("shing"));    //logs 'True' always
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经采取了正则表达式从这个源- 密码必须为8个字符,包括1个大写字母,特殊字符,字母数字字符

问题是它总是返回'True'并且我发送给方法的字符串无效.

如果我正在使用正则表达式做错了,请帮助我.

请在这里玩 - https://dotnetfiddle.net/lEFYGJ

c# regex

6
推荐指数
3
解决办法
2万
查看次数

标签 统计

c# ×1

regex ×1