Solved

c# textbox validation

Posted on 2009-04-04
4
470 Views
Last Modified: 2012-05-06
i wanna validate my textbox to only allow/accept interger
0
Comment
Question by:-Huey-
4 Comments
 
LVL 6

Accepted Solution

by:
Ramone_Hamilton earned 84 total points
ID: 24070849
Are you creating a webform or a webapplication.  If you are creating a web application use the numbervalidator control, if you are creating a winform you can check this on the KeyPress event. Look at the code below
protected void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    int numIsInt = 0; 
    e.Handled = !int.TryParse(e.KeyChar.ToString(), out numIsInt);
}

Open in new window

0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 83 total points
ID: 24071786
You could use the NumericUpDown control. Also have a look at http://www.emoreau.com/Entries/Articles/2003/03/Creating-your-own-Windows-Custom-Control.aspx
0
 
LVL 6

Assisted Solution

by:nandithaa
nandithaa earned 83 total points
ID: 24847735
Try this:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar > 47 && e.KeyChar < 58) || (e.KeyChar == 8))
            {
                e.Handled = false;
            }

            else
            {
                e.Handled = true;
            }
        }
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question