Strong Password RegEx

  • Thread starter Thread starter UJ
  • Start date Start date
U

UJ

Does anybody have lying around a Regex for a strong password (or a routine
to check it). I'd like the normal First character has to be a character,
total of 8 characters and has to have at least one number in it.

TIA - Jeff.
 
Jeff,

That's not exactly "strong". It should be a minimum of eight
characters, as well as have one letter, one number, and one special
character (non alpha-numeric).
 
Since you don't care about it following a particular pattern, just
want to make sure it has a variety of characters it's easier to just
use a loop than to use Regex. Also for "strong" password you usually
want to test for a non-alphanumeric character as well.

HTH,

Sam

using System;

namespace CommandTest
{
public class StrongPassword
{
public static bool IsStrongPassword(string password)
{
if (password == null || password.Length < 8)
{
return false;
}

// custom rule, first char must be a letter
if (!Char.IsLetter(password[0]))
{
return false;
}

bool hasLetter = false;
bool hasDigit = false;
bool hasOther = false;

foreach(char c in password)
{
if (Char.IsLetter(c))
{
hasLetter = true;
}
else if (Char.IsDigit(c))
{
hasDigit = true;
}
else
{
hasOther = true;
}
}

return hasLetter && hasDigit && hasOther;
}

public static void Test()
{
Test("hey");
Test("password");
Test("pa$$word");
Test("pa$$w0rd");
}

public static void Test(string password)
{
Console.WriteLine("{0,-12}: {1}", password,
IsStrongPassword(password));
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top