Code for TextBox validations in .Net


TextBox Control :
This control is able to enter the data by the user and send from the textbos to anywhere like saving to database, send to another textbox, insert to datagridview cells etc. 
TextBox validation for restrict Lettres :
This code will not allow the lettres from A to Z . Except those remaining charecters, symbols and special charecters will be allowed.



Event :-The code should write in TextBox KeyPress event.

       if (char.IsLetter(e.KeyChar))
            {
                e.Handled=true;
            }



TextBox validation for restrict Numbers :

This code will not allow the numeric values from 0 to 9. Except those remaining charecters, symbols and special charecters
will be allowed.

Event :-The code should write in TextBox KeyPress event.

       if (char.IsNumber(e.KeyChar))
            {
                e.Handled=true;
            }

TextBox validation for First letter is Upper case(Capital lettre) :

This code will make the first charecter to uppercase automatically, no need to press Caps Lock or Shift buttons.

Event :-The code should write in TextBox KeyPress event.

        if (textBox1.SelectionStart == 0)
            {
                e.KeyChar = char.ToUpper(e.KeyChar);
            }

1 comment:

  1. This is awesome!! really helpful for me. Thanks for sharing with us. Following links also helped me to complete my task.

    http://www.mindstick.com/Blog/322/TextBox%20Validation%20in%20C%20Net

    http://www.codeproject.com/Articles/220519/Numbers-or-Characters-only-Textbox-Validation-in-C

    ReplyDelete