I'm working with nicks (first names).
When a user registers, he must enter his nickname, he can not contain symbols (except the underscore), only numbers and letters.
I use the event KeyPress
of my TextBox
Username for this:
private void Username_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsLetterOrDigit(e.KeyChar)) this.Handled = false;
else
{
if (e.KeyChar == '\b') this.Handled = false; //Tecla de borrado
else
{
if (e.KeyChar == '_' && !((TextBox)sender).Text.Contains("_") && ((TextBox)sender).Text.Length > 0) this.Handled = false;
else this.Handled = true;
}
}
e.Handled = Handled;
}
This piece of code prevents symbols (other than "_") from being written, content starting with "_" and using more than one script under "H_O_L_A", but I need to prevent the underscore from being can use at the end, that is:
Allow: Hol_a
Prevent: Hello _
How can I get this?