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);
        }

Last updated:9/18/2014 1:24:03 PM
AVADHESH PATEL

AVADHESH PATEL

Avadhesh Kumar Patel District Project Manager - Aligarh 14 months work experience in Panchayati Raj Department Sector as District Project Manager & 12 months work experience in IT Sector as Software Engineer. :-)

2 Comments

Leave Comment