Click here to Skip to main content
15,894,343 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 19:43pm
v3

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

or:
C#
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[^]
 
Share this answer
 
Comments
Bhavna v 29-Mar-11 1:27am    
Thank Sir
m@dhu 29-Mar-11 1:31am    
Simple!.
Hi, Bhavna
Try a easy solution
C#
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
 
Share this answer
 
v2
 
Share this answer
 
v3
Comments
m@dhu 29-Mar-11 1:31am    
Fixed the links.
Hope this[^] might help you.
 
Share this answer
 

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