How to validate textBox so that it accepts point, comma, and only numbers in a textbox?

0

Today I come with a problem that I have not been able to solve, I have a textbox that should only be able to insert numbers, commas, and points.

I have a validation but this validation allows to insert signs of admiration, interrogation, among others.

I'm doing the validation with the KeyPress event

       if (Char.IsNumber(e.KeyChar))
        {
            e.Handled = false;
        }
        else if (Char.IsControl(e.KeyChar))
        {
            e.Handled = false;
        }
        else if (Char.IsPunctuation(e.KeyChar))
        {
            e.Handled = false;
        }
        else
        {
            toolTip1.IsBalloon = true;
            toolTip1.Show("Solo se permiten numeros", txtLimite, 3000);
            e.Handled = true;
        }

Expected example: 1,500.50

Example of error: 1,500.00!?!? among other signs.

    
asked by Luis Fernando 21.01.2018 в 17:17
source

1 answer

2

Yes, IsPunctuation supports other punctuation in addition to the comma and period.

You should directly compare the typed character with these two specific characters:

if (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar)
    || e.KeyChar == ',' || e.KeyChar == '.')
{
    e.Handled = false;
}
else
{
    toolTip1.IsBalloon = true;
    toolTip1.Show("Solo se permiten numeros", txtLimite, 3000);
    e.Handled = true;
}
    
answered by 21.01.2018 / 21:49
source