Click here to Skip to main content
15,886,873 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.
Posted
Comments
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 (http://blog.scosby.com/post/2010/02/11/Validation-in-Windows-Forms.aspx). For ASP.Net you can refer to this MSDN documentation (http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx) or this CP article (http://www.codeproject.com/Articles/426761/Exploring-ASP-NET-Validation-Controls)

Regards,

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.

—SA
 
Share this answer
 
you can do it by regular expression it may be windows or web.

i will give you the link. just go through


https://gkskonline.wordpress.com/2013/05/20/email-validation-using-regex/[^]
 
Share this answer
 
if you work with WinForms it's possible to use MaskedTextBox control with Mask property set into 000000 for numbers from 0 to 999999
 
Share this answer
 
v2
try this
C#
private void textBox1_KeyPress_1(object sender, KeyPressEventArgs e)
       {
           if (!(Char.IsLetter(e.KeyChar) || Char.IsControl(e.KeyChar)))
           {
               e.Handled = true;
        }
       }
 
Share this answer
 
Comments
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