i have class called passwordvalidationrules
it's 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 check rules user types passwordbox.
i have trigger when 8 characters typed , background change green if "true", otherwise should have red background.
i barely know wpf
, have done way,
first create password validation extension 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 passwordchanged
event of 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; }
Comments
Post a Comment