Regex for Password RRS feed

  • Question

  • Hi,

    I would like to ask how can I have a Regex for password to prevent any space in the password and to accept only numbers OR letters OR alphanumeric, minimum 6 and max of 20 characters?



    Monday, July 16, 2018 8:30 AM

All replies

  • Hi Jassim,

    Please check the following code.

    string text = "One123";
    string pat = @"^[a-zA-Z0-9]{6,20}";
    var m = Regex.IsMatch(text, pat);

    Best regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 16, 2018 9:24 AM
  • Please, check ist out:
    public static void Test2()
        Regex regex = new Regex(@"^[A-Za-z0-9]{6,20}$");
        Console.WriteLine("Insert password:");
        string name = Console.ReadLine();
        while (name.ToLower() != "x")
    	if (regex.IsMatch(name))
    	    Console.WriteLine($"Is valid password: {name}");
    	    Console.WriteLine($"{name} is not a valid password.");
    	Console.WriteLine("Insert password:");
    	name = Console.ReadLine();
    Monday, July 16, 2018 9:36 AM
  • For more letters and digits, try this expression too:


    Monday, July 16, 2018 11:20 AM