C# Text Box Validation




Today we will see how to make a textbox that only accepts numbers or only characters in C#.Use this Validation in TextBox KeyPress Event.




After applying this below code .you can type only words in the Text box. you can't type Numbers and Other symbols in that Text box

            if(!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar))

            {

                e.Handled = true;

            }

           
After applying this below code .you can type only Numbers in the Textbox. you can't type Words and Other symbols in that textbox

            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))

            {

                e.Handled = true;

            }
       

After applying this below code .you can type  Numbers and Words in the Textbox. you can't type Other symbols in that textbox.
 
            // allow digit + char + white space

            if (!char.IsControl(e.KeyChar) && !char.IsLetterOrDigit(e.KeyChar) && !char.IsWhiteSpace(e.KeyChar))

            {

                e.Handled = true;

            }

       

Post a Comment

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget