visual C #. Validate data inserted in textbox

0

I look for the way to validate data entered into a textbox which is used to get the value entered and take it to mathematical formulas. Now create a function which the user can only enter numbers (0-9), and the characters ".", "," And "-" to write decimals and negatives, but my problem is: when the user enters something like: "..-", ",.,", "-." the program gives error. Could you help me? Thanks in advance. I leave the function to validate as I have until this minute

public static void SoloNumeros(KeyPressEventArgs V)


{
            if (Char.IsDigit(V.KeyChar))
            {
                V.Handled = false;
            }
            else if (Char.IsControl(V.KeyChar))
            {

                V.Handled = false;
            }
            else if (Char.IsControl(V.KeyChar))
            {
                V.Handled = false;            
            }
            else if (V.KeyChar.ToString().Equals("."))
            {

                V.Handled = false;
            }
            else if (V.KeyChar.ToString().Equals(","))
            {

                V.Handled = false;
            }

            else if (V.KeyChar.ToString().Equals("-"))
            {

                V.Handled = false;
            }
            else
            {
                V.Handled = true;
                MessageBox.Show("Por favor, introduzca solo nùmeros.");
            }
        }
    
asked by HOLDTHEDOOR 23.05.2018 в 17:33
source

3 answers

0

You can use the Tryparse method. in your case the bool method (why double accepts is pairing layers ".")

would be something like this:

 int numero;
 bool esNumerico = bool.TryParse(TextBox.ToString(), out numero);

 if(esNumerico == false)
 {
        V.Handled = true;
        MessageBox.Show("Por favor, introduzca solo nùmeros.");
 }

Tryparse is a function that tries to convert the first string parameter into a number.

If Tryparse is successful, the out parameter will be loaded with the value of the conversion and the result of the function will be "true"

but if Tryparse fails, the parameter out will be null and the result of the function will be false

Now I see another problem, and that is that you are trying to validate character by character,

you can not do this in any way to find out if your result is numeric or not, you'd better try a tryparse in it with all text textbox and not character by character.

    
answered by 23.05.2018 / 17:38
source
0

make the following code that can help you with your question, for your static method of SoloNumeros perform the following code which simplifies more your code

        Regex regex = new Regex(@"^*[0-9,\.\-]+$");
        if (regex.IsMatch(V.KeyChar.ToString()) || V.KeyChar.ToString() == "\b")
        {
            V.Handled = false;

        }
        else
        {
            V.Handled = true;
            MessageBox.Show("Por favor, introduzca solo nùmeros.");
        }

To avoid that the number is wrong at the time of performing the operation and avoid passing through the code a number with the following characteristics 3..4 make the following code.

     public bool validarNumeroAntesDeOperacion(string numero)
     {
        bool EsValido;
        try
        {
            double Numero = Convert.ToDouble(numero);
            EsValido = true;
        }
     catch (Exception e)
        {

            MessageBox.Show(e.ToString());
            EsValido = false;
        }

        return EsValido;
        }

This code returns a true or false if the number is correct or incorrect, in your case it uses a double that will help a lot, if you are looking to do it in real time I recommend you to make a regex expression that allows you to validate that it does not the characters are repeated.

    
answered by 24.05.2018 в 18:32
0

Mike, thank you very much for your answer, I did not know the double.TryParse method, it was very helpful, I found the solution to my problem create the following method:

// Class Validate

public bool validacion2(string value ) 
    {
        bool esNumerico= false;
        double numero;
        if(Double.TryParse(value ,  out numero))
        {
        esNumerico=true;            
        }
        return esNumerico;           
    }

This method is used in the eleventh click of a "Calculate" button that I have in my program. I'm something else less like that

public void button1_Click(object sender, EventArgs e)
{
     Validar a1 = new Validar();
     if ((a1.validacion2(textBox1.Text) == false)
         MessageBox.Show("Existen valores no coherentes. Por favor, revise los datos insertados.");
     else
     {
        //continua el programa 
     }
}
    
answered by 24.05.2018 в 18:36