I'm doing a project in C# that includes table and user management. In my custom control TextBox I need to validate whether the user introduces a valid Spanish DNI or not. The DNI format is 8 numbers and 1 letter. The method has to unable writing if the user types more than 8 numbers or more than one letter in this order (12345678A). If someone could help i would be very grateful! Thanks for reading.
I have tried this Regular Expressions method but it doesn't work.
private void ValidateText(object sender, KeyPressEventArgs e)
{
string pattern = @"^((\d{8})|(\d{8}([A-Z]|[a-z])))$";
Regex r = new Regex(pattern);
if ((r.IsMatch(this.Text)))
{
e.Handled = true;
}
}