2011-12-05 3 views
0

J'ai un petit problème ici. Voici ma situation:Comment revérifier le champ A du champ B en utilisant TextChanged?

Je tape le nom d'utilisateur: testeur (nom d'utilisateur valide, en évitant tous les contrôles), puis tapez mot de passe: testerr (mot de passe valide, en évitant tous les contrôles). Le problème est que je vérifie les mêmes entrées. Et dans mon code lorsque les deux entrées sont les mêmes, je verrai une notification. Maintenant, quand je tape testeur comme nom d'utilisateur et mot de passe, j'obtiens l'erreur, mais quand j'ajoute un caractère supplémentaire à mon mot de passe 'testerr', je valide le mot de passe, mais le nom d'utilisateur est invalide. ma validation impossible.

Comment peut-on éviter cela? Je pensais à revérifier le champ du nom d'utilisateur du champ 2, mais je ne sais pas comment.

bool passIsValid, userIsValid; 

public AuthenticationWindow() 
{ 
    InitializeComponent(); 

    // Creating TextChanged events which till validate input fields. 
    txtUserName.TextChanged += new EventHandler(txtUserName_TextChanged); 
    txtPassword.TextChanged += new EventHandler(txtPassword_TextChanged); 
} 

private void txtUserName_TextChanged(object sender, EventArgs e) 
{ 
    // Checking for empty user name field. 
    if (string.IsNullOrEmpty(txtUserName.Text)) 
    { 
     lblMessageUser.Text = "User Name field cannot be empty!"; 
     lblMessageUser.ForeColor = Color.IndianRed; 
     btnAuthenticate.Enabled = false; 
     userIsValid = false; 
    } 
    // Making sure that user name is at least 6 characters long. 
    else if (txtUserName.Text.Length < 6) 
    { 
     lblMessageUser.Text = "User Name field must be at least 6 characters long!"; 
     lblMessageUser.ForeColor = Color.IndianRed; 
     btnAuthenticate.Enabled = false; 
     userIsValid = false; 
    } 
    // Checking for user name made of same repeating character. 
    // Invalid example: 'aaaaaa' 
    else if (!txtUserName.Text.Distinct().Skip(1).Any()) 
    { 
     lblMessageUser.Text = "User Name cannot be made of repeating the same characters!"; 
     lblMessageUser.ForeColor = Color.IndianRed; 
     btnAuthenticate.Enabled = false; 
     userIsValid = false; 
    } 
    // Making sure that password and user name aren't the same. 
    else if (txtUserName.Text == txtPassword.Text) 
    { 
     lblMessageUser.Text = "User Name and Password can not be the same!"; 
     lblMessagePass.Text = "User Name and Password can not be the same!"; 
     lblMessageUser.ForeColor = Color.IndianRed; 
     lblMessagePass.ForeColor = Color.IndianRed; 
     btnAuthenticate.Enabled = false; 
     userIsValid = false; 
     passIsValid = false; 
    } 
    // If all other checks aren't trigered; enable authentication. 
    else 
    { 
     lblMessageUser.Text = "User Name is valid."; 
     lblMessageUser.ForeColor = Color.Green; 

     userIsValid = true; 

     if (passIsValid && userIsValid) 
     { 
      btnAuthenticate.Enabled = true; 
     } 
    } 
} 

private void txtPassword_TextChanged(object sender, EventArgs e) 
{ 
    // Checking for Null or Empty string in password field. 
    if (string.IsNullOrEmpty(txtPassword.Text)) 
    { 
     lblMessagePass.Text = "Password field cannot be empty!"; 
     lblMessagePass.ForeColor = Color.IndianRed; 
     btnAuthenticate.Enabled = false; 
     passIsValid = false; 
    } 
    // Making sure that password is at least 6 characters long. 
    else if (txtPassword.Text.Length < 6) 
    { 
     lblMessagePass.Text = "Password field must be at least 6 characters long!"; 
     lblMessagePass.ForeColor = Color.IndianRed; 
     btnAuthenticate.Enabled = false; 
     passIsValid = false; 
    } 
    // Checking for password made of same repeating character. 
    // Invalid example: 'aaaaaa' 
    else if (!txtPassword.Text.Distinct().Skip(1).Any()) 
    { 
     lblMessagePass.Text = "Password cannot be made of repeating the same characters!"; 
     lblMessagePass.ForeColor = Color.IndianRed; 
     btnAuthenticate.Enabled = false; 
     passIsValid = false; 
    } 
    // Making sure that user name and password are not the same. 
    // Security measure. 
    else if (txtUserName.Text == txtPassword.Text) 
    { 
     lblMessageUser.Text = "User Name and Password can not be the same!"; 
     lblMessagePass.Text = "User Name and Password can not be the same!"; 
     lblMessageUser.ForeColor = Color.IndianRed; 
     lblMessagePass.ForeColor = Color.IndianRed; 
     btnAuthenticate.Enabled = false; 
     userIsValid = false; 
     passIsValid = false; 
    } 
    // If all other checks aren't trigered; enable authentication. 
    else 
    { 
     lblMessagePass.Text = "Password is valid."; 
     lblMessagePass.ForeColor = Color.Green; 

     passIsValid = true; 

     if (passIsValid && userIsValid) 
     { 
      btnAuthenticate.Enabled = true; 
     } 
    } 
} 
+1

Le testeur ne comporte pas plus de 5 caractères, échouant ainsi à la vérification de "plus de 6". Si vous déboguez les méthodes, où l'indicateur userIsValid est-il défini? –

+0

@Sebastian Piu: Testeur est de 6 caractères et l'entrée ne peut pas être «plus courte» que 6 caractères. 'userIsValid' et 'passIsValid' sont définis dans les instructions 'else' au bas de chaque méthode. – HelpNeeder

+0

hah vous avez raison, je suis encore un peu endormi. –

Répondre

1

Vous pouvez fusionner vos deux événements en un, puis l'utilisateur voit toutes les erreurs et votre vérification peut aboutir.

bool passIsValid, userIsValid; 

public AuthenticationWindow() 
{ 
    InitializeComponent(); 

    // Creating TextChanged events which till validate input fields. 
    txtUserName.TextChanged += new EventHandler(txtCheck_TextChanged); 
    txtPassword.TextChanged += new EventHandler(txtCheck_TextChanged); 
} 

private void txtCheck_TextChanged(object sender, EventArgs e) 
{ 
    // Checking for empty user name field. 
    if (string.IsNullOrEmpty(txtUserName.Text)) 
    { 
     lblMessageUser.Text = "User Name field cannot be empty!"; 
     lblMessageUser.ForeColor = Color.IndianRed; 
     btnAuthenticate.Enabled = false; 
     userIsValid = false; 
    } 
    // Making sure that user name is at least 6 characters long. 
    else if (txtUserName.Text.Length < 6) 
    { 
     lblMessageUser.Text = "User Name field must be at least 6 characters long!"; 
     lblMessageUser.ForeColor = Color.IndianRed; 
     btnAuthenticate.Enabled = false; 
     userIsValid = false; 
    } 
    // Checking for user name made of same repeating character. 
    // Invalid example: 'aaaaaa' 
    else if (!txtUserName.Text.Distinct().Skip(1).Any()) 
    { 
     lblMessageUser.Text = "User Name cannot be made of repeating the same characters!"; 
     lblMessageUser.ForeColor = Color.IndianRed; 
     btnAuthenticate.Enabled = false; 
     userIsValid = false; 
    } 
    else 
    { 
     userIsValid = true; 
     lblMessageUser.Text = "Password is valid."; 
     lblMessageUser.ForeColor = Color.Green; 
    } 

    // Checking for Null or Empty string in password field. 
    if (string.IsNullOrEmpty(txtPassword.Text)) 
    { 
     lblMessagePass.Text = "Password field cannot be empty!"; 
     lblMessagePass.ForeColor = Color.IndianRed; 
     btnAuthenticate.Enabled = false; 
     passIsValid = false; 
    } 
    // Making sure that password is at least 6 characters long. 
    else if (txtPassword.Text.Length < 6) 
    { 
     lblMessagePass.Text = "Password field must be at least 6 characters long!"; 
     lblMessagePass.ForeColor = Color.IndianRed; 
     btnAuthenticate.Enabled = false; 
     passIsValid = false; 
    } 
    // Checking for password made of same repeating character. 
    // Invalid example: 'aaaaaa' 
    else if (!txtPassword.Text.Distinct().Skip(1).Any()) 
    { 
     lblMessagePass.Text = "Password cannot be made of repeating the same characters!"; 
     lblMessagePass.ForeColor = Color.IndianRed; 
     btnAuthenticate.Enabled = false; 
     passIsValid = false; 
    } 
    else 
    { 
     passIsValid = true; 
     lblMessagePass.Text = "Password is valid."; 
     lblMessagePass.ForeColor = Color.Green; 
    } 

    // Making sure that user name and password are not the same. 
    // Security measure. 
    if (txtUserName.Text == txtPassword.Text) 
    { 
     lblMessageUser.Text = "User Name and Password can not be the same!"; 
     lblMessagePass.Text = "User Name and Password can not be the same!"; 
     lblMessageUser.ForeColor = Color.IndianRed; 
     lblMessagePass.ForeColor = Color.IndianRed; 
     btnAuthenticate.Enabled = false; 
     userIsValid = false; 
     passIsValid = false; 
    } 

    // If all other checks aren't trigered; enable authentication. 
    if (passIsValid && userIsValid) 
    { 
     btnAuthenticate.Enabled = true; 
    } 
} 
+0

Parfait! Je ne savais pas que je pouvais faire ça. Merci beaucoup! – HelpNeeder

Questions connexes