Click here to Skip to main content
13,143,637 members (29,259 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Wish to use text box to only allow numeric Data entry

Can any one help me with code for restricting other than numbers and backspace in text box.
Posted 8-Jun-11 22:24pm
Ragi Gopi1.3K
Updated 28-Mar-17 18:55pm
v3
Comments
walterhevedeich 9-Jun-11 4:27am
   
Tag it. Windows forms or web application?
Ragigopi 9-Jun-11 4:28am
   
Windows forms
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

You could use a MaskedTextBox[^] with a Mask of "####" - that is what it's there for...
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

On KeyPress event write following code

if (char.IsNumber(e.KeyChar))
     e.Handled = false;
else
     e.Handled = true;
  Permalink  
Comments
RakeshMeena 9-Jun-11 4:42am
   
My 5! Though you don't need to put both If and else parts.
CS2011 9-Jun-11 4:47am
   
Thanks.
Tarun.K.S 9-Jun-11 4:59am
   
I beg to differ here, else *is* required.
CS2011 9-Jun-11 5:14am
   
What Else ?? :-)
Tarun.K.S 9-Jun-11 5:28am
   
Hahahhaaa! :D
Tarun.K.S 9-Jun-11 4:59am
   
Good answer Chanakya :). 5+
CS2011 9-Jun-11 5:14am
   
Thanks
Tarun.K.S 9-Jun-11 5:03am
   
I have one small doubt. If a user pastes any character(s) into the textbox(right-click on textbox, then paste), it unfortunately shows the character(s). Any suggestion how this can be prevented? Thank you.
CS2011 9-Jun-11 5:27am
   
you can disable to paste option if you want :-)
Tarun.K.S 9-Jun-11 5:29am
   
Lol that's the only solution in sight. :D
RakeshMeena 9-Jun-11 5:41am
   
Nop! Solutions are still available but a bit complex.
There are 2 ways you can paste in a textbox:
1. Ctrl + V : This is already disabled (because of original solution), so no problem.
2. Mouse right click and paste: Here we can handle MouseDown event and if the right button is clicked, save the textbox's text in a variable. Then on the TextChanged event, check if the text contains only numbers or not, if not previous value can be restored.

Hope that helps.
Tarun.K.S 9-Jun-11 7:30am
   
Aah that's a feasible solution, thanks for the heads up. :)
CS2011 9-Jun-11 7:05am
   
As Rakesh Told Ctrl+V is disabled and if you add
this.textBox1.ContextMenu = new ContextMenu();InitializeComponent() Then Your problem is solved :-). See not so hard afte all :-D
Tarun.K.S 9-Jun-11 7:32am
   
Another excellent solution, create a custom ContextMenu and don't add items in it which makes it feel like disabled. Perfect. Thanks! :)
Ragigopi 9-Jun-11 5:50am
   
How can we disable Paste option????
CS2011 9-Jun-11 7:03am
   
Ctrl+V is disabled already. Now if you want to disable Right Mouse Click Menu(i.e Copy,Past,etc) add following code this.textBox1.ContextMenu = new ContextMenu();
Ragigopi 9-Jun-11 7:36am
   
private void tbph_MouseClick(object sender, MouseEventArgs e)
{
this.tbph.ContextMenu = new ContextMenu();
}

Got Ans...
@CS2011
thankuuuu
Ragigopi 9-Jun-11 6:02am
   
thankuuuuu.....
its workssss
thank u all
Ragigopi 9-Jun-11 6:47am
   
if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar != '\b'))
{
MessageBox.Show("Only numbers Allowed");
e.Handled = true;
}
else
{
e.Handled = false;
}

atlast every thing is fine....
CS2011 9-Jun-11 6:56am
   
Yep..Otherwise backspace will not work.I knew that but wanted you to try it by your self.Good Work :-)
Ragigopi 9-Jun-11 7:03am
   
:)
Amit Kumar Tiwari 9-Jun-11 9:06am
   
Good answer, my 5 is in :)
CS2011 9-Jun-11 9:21am
   
Thanks Amit
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

You need a Masked textbox.

Here: Masked C# TextBox Control[^]

Further, you can also implement the functionality using OnKeyPress or OnKeyUp or OnBlur (there may be more events, pick any one that suits you). When the particular method event is raised, you can check for the key pressed or the text present/entered and act accordingly - show an alert/error/blankout.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

private void tbph_TextChanged_1(object sender, EventArgs e)
       {
           tbph.KeyPress += new KeyPressEventHandler(tbph_KeyPress);
       }
       private void tbph_KeyPress(object sender, KeyPressEventArgs e)
       {
           if ((e.KeyChar < '0') || (e.KeyChar > '9')) e.Handled = true;
       }



i tried above code ..
and got answer...
  Permalink  
Comments
Tarun.K.S 9-Jun-11 5:15am
   
How about copying a text, then pasting it( by right-clicking, then click paste on it). The text will appear.
Ragigopi 9-Jun-11 5:42am
   
ohhhhh..thats a problem.....
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

Javascript Function:

function isOnlyNumber(field) {
      var e = event;
      var charCode = e.which || e.keyCode;
      if (charCode > 47 && charCode < 58)
      {
      return true;
      }
      else{
      return false;
      }


Use This Function in TextBox onkeypress event as below.

<asp:TextBox ID="txt1" runat="server" CssClass="inputbox" onkeypress="return isOnlyNumber(this);"></asp:TextBox>
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 29 Mar 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100