create a password with at least one number in csharp

To create a password with at least one number in C#, you can use Regular Expressions to perform the validation of the password.

Here is an example code that validates if the password contains at least one number:

main.cs
using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string password = "myPassword123";
        
        if (Regex.IsMatch(password, @"[0-9]+"))
        {
            Console.WriteLine("Password is valid");
        }
        else
        {
            Console.WriteLine("Password must contain at least one number");
        }
    }
}
401 chars
20 lines

In the example above, we define a regular expression @"[0-9]+" that matches at least one number in the password. We use the Regex.IsMatch() method to check if the password matches this regular expression.

If the password matches the regular expression, we print "Password is valid". Otherwise, we print "Password must contain at least one number".

gistlibby LogSnag