简体   繁体   中英

How to Accept '(“ and ”)" on Digits Only Accepted Textbox

I have field where only digits are accepted but i want to same feature for another field (fax) where example values will look like (070) 412 34 56.

how can modify my current function so it also accepts "(" and ")" ?

here is current code i have to check digits

char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8 && ch != 46)
{
    e.Handled = true;
}

e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);

One possibility:

Place every allowed character in a string and see if the entered character exists.

e.Handled = "0123456789()".IndexOf(e.KeyChar) < 0;

string.IndexOf returns the index of the given char.
It returns -1 if the character was not found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM