If the Password boxes are empty. It shows Passwords created Successfully
. I dunno whats wrong I did here. But, if entered passwords are not matched, if condition works as per code. Error for validating null.
Code;
void savePassword(object sender, RoutedEventArgs e)
{
string password1 = createPassword.Password;
string password2 = repeatPassword.Password;
if (password1 != null || password2 != null)
{
if (password1 == password2)
{
MessageBox.Show("Password Created Successfully");
}
else
{
MessageBox.Show("Passwords did not match.");
}
}
else
{
MessageBox.Show("Both fields are required");
}
}
You can check if the PasswordBox is filled properly with the string.IsNullOrWhiteSpace or string.IsNullOrEmpty methods. Also, you might want to remove the != because your logic validates as soon as one of the PasswordBoxes has content.
Here's a sample:
//Is true only if both passwords have content
if(!string.IsNullOrWhiteSpace(password1) && !string.IsNullOrWhiteSpace(password2))
尝试这个
if (password1.Length == 0 || password2.Length == 0)
You have to check that password is empty or null
try this
if (!string.IsNullOrEmpty(password1) &&!string.IsNullOrEmpty(password2))
{
MessageBox.Show("Password Created Successfully");
}
else
{
MessageBox.Show("Passwords did not match.");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.