Password confirm in WPF

In WPF the PasswordBox control doesn’t expose any property to bind it. You’ll need solutions as described here to get the password the ‘WPF way’. Getting it to validate is a different story.
Whenever a user is created you want the password to be confirmed. That was, is and allways will be the way to make sure the user knows the password. Mistakes are quickly made and this way the mistake has to be made twice before it becomes a problem. But how to compare two PasswordBox controls that use the passwordhelper?
Use the IDataErrorInfo to implement your own validation. A sample implementation is below. Ignore the Error property of the interface as we don’t use it in this implementation.

public string this[string columnName]
{
    get
    {
        string result = null;
        // columName is the name of the property whose error message to get
        switch (columnName)
        {
            case "PasswordConfirm":
                // only check if password is supplied, else confirm is obsolete
                // password must be equal to confirm
                if (string.IsNullOrEmpty(Password) == false && 
                    Password.Equals(PasswordConfirm) == false)
                {
                    result = "Passwords differ";
                }
                break;
        }
        return result;
    }
}

A complete sample is available for download here.

About erictummers

Working in a DevOps team is the best thing that happened to me. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development and tagged , , , . Bookmark the permalink.

1 Response to Password confirm in WPF

  1. Christoph Wolf says:

    sadly, that doesn’t cover when the password itself is changed and the password confirmation was matching the password before…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.