Reputation: 7625
I have a class called PasswordValidationRules
It's a simple class
public bool validatePasswordRules(String text)
{
Regex regex = new Regex(@"^(?=.{8}$)(?=.*[A-Z])(?=.*[0-9])(?=.*[,@#$])");
bool isValid = regex.IsMatch(text);
if (isValid)
{
return true;
}
else
{
return false;
}
}
I want to check the rules as the user types into a PasswordBox.
I would like to have it trigger only when 8 characters are typed and the background would change to green if "true", otherwise it should have a red background.
Upvotes: 2
Views: 1984
Reputation: 172
I use this as validating event so when press tab it changes the color, but if you want you can use it inside the button event , it will work the same, when you press the button it will verify the length of the password box and if its more then 8 it will change the color to green.
Upvotes: 0
Reputation: 172
This is a textbox which acts as password box , it uses a password char and this code is in c#, use the textbox and change its propert to password char and define the password character so it will display that character instead of text, and so it use as pasword box , as in wpf
Upvotes: 0
Reputation: 172
by reading your question i think you should try this in c# take a picture box behind the textbox and validate the textbox length, if the lenght is less then 8 characters change the backcolor of the picturebox to red and if its >= 8 then change the color to green ,Here is the code example :
if (textBox1.Text == "")
{
pictureBox1.BackColor = Color.Transparent;
}
else if (textBox1.Text.Length >= 8)
{
pictureBox1.BackColor = Color.Green;
}
else if ( textBox1.Text.Length < 8)
{
pictureBox1.BackColor = Color.Red;
}
use this code on validating event of your password textbox, and change the name of textbox1 to your textbox name Hope this will help you to solve your problem.
Upvotes: 0
Reputation: 1950
I barely know WPF
, but I would have done this way,
First create password validation extension for String
public static class Validate
{
public static bool ValidatePassword(this String password)
{
Regex regex = new Regex(@"^(?=.{8}$)(?=.*[A-Z])(?=.*[0-9])(?=.*[,@#$])");
bool isValid = regex.IsMatch(password);
if (isValid)
{
return true;
}
else
{
return false;
}
}
}
Then in the PasswordChanged
event of the PasswordBox
,
private void text_PasswordChanged(object sender, RoutedEventArgs e)
{
if (text.Password.Length >= 8)
{
if (text.Password.ValidatePassword())
{
text.Background = new SolidColorBrush(Color.FromRgb(0, 255, 0));
}
else
text.Background = new SolidColorBrush(Color.FromRgb(255, 0, 0));
}
else
text.Background = SystemColors.WindowBrush;
}
Upvotes: 1