TextBox with only numbers

I need to create a TextBox with only numbers but I couldn’t do. I have tried to put : InputScope = “Numbers” but this only work on Mobile. Also I have tried on TextChanging this:

private void textBox1_TextChanged(object sender, EventArgs e)
    if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[^0-9]"))

        textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);


You can either prevent any non-numeric input whatsoever, or just filter out digits in the text.

Preventing non-digit input

Use the BeforeTextChanging event:

<TextBox BeforeTextChanging="TextBox_OnBeforeTextChanging" />

And now handle like this:

private void TextBox_OnBeforeTextChanging(TextBox sender,
                                          TextBoxBeforeTextChangingEventArgs args)
    args.Cancel = args.NewText.Any(c => !char.IsDigit(c));

This LINQ expression will return true and hence Cancel the text change in case it encounters any non-digit character in the input.

Filtering non-digit input

Use the TextChanging event:

<TextBox TextChanging="TextBox_OnTextChanging" /> 

And handle this way:

private void TextBox_OnTextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
    sender.Text = new String(sender.Text.Where(char.IsDigit).ToArray());

This LINQ query will filter out non-digit characters and create a new string only with the digits in the input.

It is preferable to use TextChanging and BeforeTextChanging, because TextChanged occurs too late, so the user would be confused by seeing characters temporarily display on the screen and immediately disappearing.

Leave a Reply

Your email address will not be published. Required fields are marked *