Valid with the following method in C # with a WPF window a text that should only accept valid names, as well as accept 2 names but no more, such as the following:
Correct examples:
- Alejandro
- Richard Yordy
Erroneous examples:
- 2Alejandro24
- Teresa de jesus
Code
if (e.Key == Key.Delete || e.Key == Key.Back)
{
if (tbNombre.Text.Length >= 1|| tbNombre.Text.Length ==0)
{
nombre = tbNombre.Text;
}
}
else if (Regex.IsMatch(tbNombre.Text, "^([a-zA-Z]{1,10}[ ]?[a-zA-Z]{0,7})$"))
{
nombre = tbNombre.Text;
}
else if (tbNombre.Text.Length != 0)
{
tt.Content = "No se aceptan numeros";
tbNombre.ToolTip = tt;
tt.IsOpen = true;
int cursor = tbNombre.SelectionStart;
tbNombre.Text = nombre;
timer.Tick += new EventHandler(delegate (object timerSender, EventArgs timerArgs)//Para desaparecer el tooltip en un determinado tiempo
{
tt.IsOpen = false;
});
if (cursor >= 0)
{
if (cursor == 0)
{
tbNombre.SelectionStart = cursor;
}
else
{
tbNombre.SelectionStart = cursor - 1;
}
}
}
I solve it in this way which validates with form is written to prevent invalid data being entered such as numbers. I would like to know if there is any way to improve this to implement it in other TextBox