.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Code Snippets >> Validations >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Text Box validation in C#.net

Posted By:Narayanan       Posted Date: December 22, 2011    Points: 40    Category: Validations    URL: http://www.dotnetspark.com  

Today, we will discuss about validate Textbox in C#.net.It is a simple One.Validation is a important in the Client side.So Programmer must know about validating Code.

The following code for validating text box in C#.net .before that, you know ASCII key value.

What is mean by ASCII?

ASCII --American Standard Code for Information Interchange.

Refer this link http://en.wikipedia.org/wiki/ASCII

Now , we go to our class. here i give a method for Validating text box in C#.net.

 public static bool ValidateTextBox(int keyAscii)
            if (keyAscii >= 65 && keyAscii <= 90)
                return true;
            else if (keyAscii >= 97 && keyAscii <= 122)
                return true;
            else if (keyAscii == 8 || keyAscii == 13)
                return true;
            else if  ((keyAscii  > 47 && keyAscii  < 58) || (keyAscii  == 46))
                return true;
            return false;

finally ,You call this method in your Text Box Keypress Event like this.

int x = (int)e.KeyChar;
e.Handled = !ValidateTextBox(x);
now, you run your project and see the result .


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend