Click here to Skip to main content
14,774,521 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
How to restrict a user entering character in textbox,the textbox is exclusively for entering Mobile no.
Posted
Updated 28-Mar-11 20:43pm
v3

private void txtType1_KeyPress(object sender, KeyPressEventArgs e)
{
     int isNumber = 0;
     e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber);
}

or:
private void txtType2_KeyPress(object sender, KeyPressEventArgs e)
{
     if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
          e.Handled = true;
}

See here:
http://www.vcskicks.com/numbers_only_textbox.php[^]
   
Comments
Bhavna v 29-Mar-11 1:27am
   
Thank Sir
m@dhu 29-Mar-11 1:31am
   
Simple!.
Hi, Bhavna
Try a easy solution
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= 65 && e.KeyChar <= 91) || (e.KeyChar >= 97 && e.KeyChar <= 123))
            {
                if (i && e.KeyChar == 69)
                {
                    i = false;
                }
                else if (e.KeyChar!=69)
                {
                    e.Handled = true;
                }



                }
        }


there are some more ways you should find out, it's work
   
v2
Hope this[^] might help you.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900