Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating password using regex c# [duplicate]

Tags:

c#

regex

This must be simple and I expect it working in the same way but it's not helping me out.

using System;
using System.Text.RegularExpressions;

I am in a need of password validation regular expression with certain conditions where -

1) It must contain at least a number

2) one upper case letter

3) 8 characters long.

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
    }
}

I've taken regex from this source- Password must be 8 characters including 1 uppercase letter, 1 special character, alphanumeric characters

Issue is that it returns 'True' always and the string that I am sending to method is not valid.

Help me if I am doing something wrong with the regex.

Please play with it here- https://dotnetfiddle.net/lEFYGJ

like image 392
Manoz Avatar asked Jan 11 '16 06:01

Manoz


People also ask

What are the rules of regex for a password?

This regex will enforce these rules: • At least one upper case english letter • At least one lower case english letter • At least one digit • At least one special character • Minimum 8 in length refered from : Regex for Password Must be contain at least 8 characters, least 1 number and both lower and uppercase letters and special characters

How to do password validation program in C++?

Password Validation Program in C++. Steps involved are:-The user provides the password which is to be check. The password provided by the user will now be check for uppercase, lowercase, and digits. The strongness of the password is classified into 3 categories which are, strong, moderate, and weak.

Is it clear as rain to use regex for password validation?

It is clear as rain. See Lookahead Example: Simple Password Validation if you ever have any doubt when using regex for password validation. BTW, your current code does not account for Unicode upper case letters. I have created a simple method to validate all kind of password.

How to check if a string password is valid or not?

Firstly the length of the password has to be checked then whether it contains uppercase, lowercase, digits and special characters. If all of them are present then the method isValid (String password) returns true.


1 Answers

I recommend you create separate patterns to validate the password:

var input = "P@ssw0rd";

var hasNumber = new Regex(@"[0-9]+");
var hasUpperChar = new Regex(@"[A-Z]+");
var hasMinimum8Chars = new Regex(@".{8,}");

var isValidated = hasNumber.IsMatch(input) && hasUpperChar.IsMatch(input) && hasMinimum8Chars.IsMatch(input);
Console.WriteLine(isValidated);
like image 88
tdat00 Avatar answered Oct 15 '22 12:10

tdat00