I have tried this code to restrict only numbers.It type only numbers and don\'t make entry when we try to enter characters or any other controls, even it doesnt enter backsp
Methods Suggested above only prevent user typing anything but number, but it will fail if user copy and paste some text inside textbox so we also need to check the input on text change event
Create ontextchangeEvent
private void TxtBox1_textChanged(object sender, EventArgs e)
{
if (!IsDigitsOnly(contactText.Text))
{
contactText.Text = string.Empty;
}
}
private bool IsDigitsOnly(string str)
{
foreach (char c in str)
{
if (c < '0' || c > '9')
return false;
}
return true;
}