Home > DeveloperSection > Blogs > TextBox Validation in C#.Net

TextBox Validation in C#.Net


C# C# 
Ratings:
0 Comment(s)
 11090  View(s)
Rate this:

TextBox Validation in C#.Net

TextBox Validation

In some cases I want to restricted that my textbox accept only specific input which we want. That means in name’s textbox no one can enter special and numeric etc character. Like this salary’s textbox user entered only numeric values.

Character Validation

In this case user entered only character in texbox with backspace and space buttons.

private void txtName_KeyPress(object sender, KeyPressEventArgs e)

        {

            e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space);

        }

Numeric Validation

In this case user entered only numeric values in textbox with back button.

private void txtAge_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

        {

            e.Handled = !(char.IsNumber(e.KeyChar) || e.KeyChar == (char)Keys.Back);

        }

Numeric with Decimal Validation

In this case user entered only decimal values in textbox with back button

private void txtSalary_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

        {

            e.Handled = !(char.IsNumber(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == (char)Keys.Back);

        }


Don't want to miss updates? Please click the below button!

Follow MindStick