Question

I'm creating a WPF window for insert user, I'm using PasswordBox for user type your password, but I have not ideas what I can put in passBox.Password.Contains( )

I need help for how to check this PasswordBox contains chars and numbers?

Was it helpful?

Solution

Contains is the wrong method.

Here:

bool isValidPassword = passBox.Password.Any(char.IsDigit) 
                           && passBox.Password.Any(char.IsLetter);

OTHER TIPS

You can use regular expression to check it. It will be something like this:

using System.Text.RegularExpressions;

Regex regex = new Regex(@"^.*(?=.{4,10})(?=.*\d)(?=.*[a-zA-Z]).*$");
if (regex.Match(passwordBox1.Password).Success)
{
  //the password match the rule
}

The above regular expression matches if:

  1. Search for at least one digit in any position
  2. Search for at least one upper or lower case in any position
  3. Enforce password to consist of 4-10 characters

You can modify it to fit your needs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top