Click here to Skip to main content
14,921,764 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display a textbox ,which should accept only positive integer,it should not accept any other special charecters.
please do the needful.
Rockstar_ 24-May-13 1:18am
Windows or web?
Prasad Khandekar 24-May-13 1:33am
Hello Jyothivs,

For Windows Forms you can refer to this tutorial ( For ASP.Net you can refer to this MSDN documentation ( or this CP article (


Don't validate it, filter out every characters but digits. More exactly, validate late, but first filter our unwanted characters as the user types the value in. You did not tag the UI library or application type you are using, but in all cases this is possible. Typically, this is handling of the event KeyPress.

On top of it, validate the value. First of all, if you type no digits or too many, it won't be parsed as any integer type, so first try System.UInt64.TryParse(yourTextBoxText) (or other appropriate unsigned integer type). When parsing is successful, you can additionally validate for required range of the value.

you can do it by regular expression it may be windows or web.

i will give you the link. just go through[^]
if you work with WinForms it's possible to use MaskedTextBox control with Mask property set into 000000 for numbers from 0 to 999999
try this
private void textBox1_KeyPress_1(object sender, KeyPressEventArgs e)
           if (!(Char.IsLetter(e.KeyChar) || Char.IsControl(e.KeyChar)))
               e.Handled = true;
Basmeh Awad 29-May-13 3:16am
it is not always necessary that you will get exact code as you asked in your question..sometimes you had to take idea and customise it by yourself..always you will not get a code to just copy and paste in your app and it works you have to put efforts by your side..

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