Validate a textBox in C # Visual Studio WindowsForms

0

I have a textBox called txtSSID and I want to validate that only letters (not all), numbers and some special characters are entered:

Do not allow letters like the ones I detail below (Uppercase or lowercase):

  

'é', 'ý', 'ú', 'í', 'ñ', 'ó', 'á', 'ë', 'ÿ', 'ü', 'ï', 'ö' , 'ä', 'ê', 'û', 'î', 'ô', 'â'

I have this code:

char[] NoPermitir = { 'é', 'ý', 'ú', 'í', ' ', 'ñ', 'ó', 'á', 'ë', 'ÿ', 'ü', 'ï', 'ö', 'ä','ê','û','î','ô','â'};

Allow special character like the ones I detail below:

  

'-', '_'

I have this code:

char[] Permitir = { '-', '_'};

This is the complete code that validates all the letters including ñ, accents and other Spanish characters which I do not want to be, validates numbers and admits the BakcSpace ( The key that is used to delete) this is the code:

private void txtSSID_KeyPress(object sender, KeyPressEventArgs e)
    {
        char[] NoPermitir = { 'é', 'ý', 'ú', 'í', 'ñ', 'ó', 'á', 'ë', 'ÿ', 'ü', 'ï', 'ö', 'ä','ê','û','î','ô','â'};
        char[] Permitir = { '-', '_'};

        if (!(char.IsLetter(e.KeyChar)) && (e.KeyChar != (char)Keys.Back) && !(char.IsNumber(e.KeyChar)))
        {
            MessageBox.Show("Solo se permiten letras y números", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            e.Handled = true;
            return;
        }
        if (String.IsNullOrEmpty(txtSSID.Text))
        {
            label1.Text = "Error";
        }
        else
        {
            label1.Text = "Correcto";
        }
    }

How can I also validate Char[] Permitir and Char[]NoPermitir in my textBox txtSSID .

    
asked by Juan López 08.10.2017 в 00:09
source

1 answer

0

You can do it with the function Contains in the following way:

private void txtSSID_KeyPress(object sender, KeyPressEventArgs e)
{
    char[] NoPermitir = { 'é', 'ý', 'ú', 'í', 'ñ', 'ó', 'á', 'ë', 'ÿ', 'ü', 'ï', 'ö', 'ä','ê','û','î','ô','â'};
    char[] Permitir = { '-', '_'};

    //Validamos si el carácter es un valor permitido, si es falso entramos
    if (!Permitir.Contains(e.KeyChar))
    {
        //Validamos que el valor sea un carácter no permitido
        if (NoPermitir.Contains(e.KeyChar))
        {
            MessageBox.Show("Carácter no permitido.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            e.Handled = true;
            return;
        }
        else
        {
            //Si no es un carácter no permitido validamos que sea letra y numero o la tecla back
            if (!(char.IsLetter(e.KeyChar)) && (e.KeyChar != (char)Keys.Back) && !(char.IsNumber(e.KeyChar)))
            {
                MessageBox.Show("Solo se permiten letras y números", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                e.Handled = true;
                return;
            }
        }
    }
    if (String.IsNullOrEmpty(txtSSID.Text))
    {
        label1.Text = "Error";
    }
    else
    {
        label1.Text = "Correcto";
    }
}
    
answered by 09.10.2017 / 12:53
source