I want to prohibit the input of non-Latin characters when entering username and password during registration.

I make registration in WPF and I want to warn the user that input in other languages ​​is prohibited.

  • 2
    Why such a strange condition? I hope you are not going to store the password in clear text in the database? - VladD
  • Entering a user can be checked in KeyPress, for example, with the help of the regular account: [a-zA-Z] * (finish with the required allowed characters) and respond in the way you want. - wind
  • Or simply to check that the code of each character meets the desired condition (greater than or equal to A, less equal to Z) && (greater than equals a, less than equals z). - Roman Kotenko
  • @Roman Kotenko: || ? - VladD

2 answers 2

I would do like this

 private void textBox1_TextChanged(object sender, EventArgs e) { if (!System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "^[a-zA-Z]") && textBox1.TextLength!=0) { MessageBox.Show("Π’Π²ΠΎΠ΄ Π²ΠΎΠ·ΠΌΠΎΠΆΠ΅Π½ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π½Π° Π»Π°Ρ‚ΠΈΠ½ΠΈΡ†Π΅"); textBox1.Text.Remove(textBox1.Text.Length - 1); } } 

    Here is the working code for the WinForm project. I think for wpf you will remake without difficulty. The code is working.

     private void FIOTextBox_TextChanged(object sender, EventArgs e) { bool BadSymbolsInside = false; for (int i = 0; i < FIOTextBox.Text.Length; i++) { if (!((FIOTextBox.Text[i] > 'A' && FIOTextBox.Text[i] < 'Z') || (FIOTextBox.Text[i] > 'a' && FIOTextBox.Text[i] < 'z') || //Π›ΡŽΠ±Ρ‹Π΅ Π΄Ρ€ΡƒΠ³ΠΈΠ΅ Π΄ΠΈΠ°ΠΏΠ°Π·ΠΎΠ½Ρ‹ числС ΠΏΠΎ Π²Π°ΡˆΠ΅ΠΌΡƒ вкусу || (FIOTextBox.Text[i] > '0' && FIOTextBox.Text[i] < '9'))) { BadSymbolsInside = true; } } if (BadSymbolsInside) { MessageBox.Show("Π’Ρ‹ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚Π΅ нСдопустимыС символы \nДля Π»ΠΎΠ³ΠΈΠ½Π° ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ латинскиС Π±ΡƒΠΊΠ²Ρ‹ ΠΈ Ρ†ΠΈΡ„Ρ€Ρ‹"); FIOTextBox.Text = ""; } } 
    • what cons then? - Mikhail Tatarintsev
    • 2
      It looks like go * nocode. - Breaker
    • @Breaker yes, with a problem code, but he, at least, knows how to solve this problem :) - dirkgntly