N
N
neitoo2022-02-08 20:28:04
WPF
neitoo, 2022-02-08 20:28:04

How to allow only numbers and '+' to be entered in a textBox?

A number is entered in the textBox in the format +79991112233, but if you enter the same number of letters, then there will be no error
Tried like this

if (!Char.IsDigit(e.Text, 0))
            {
                e.Handled = true;
            }

But then '+' can't be entered either

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2022-02-08
@neitoo

Something like this, only you can add one more condition that if the plus has already been entered, then do not let it be entered a second time. Or then filter and cut out unnecessary pluses.

if (!Char.IsDigit(e.Text, 0) || e.Text != '+')
            {
                e.Handled = true;
            }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question