---
title: "TextBox Validation in C#.Net"  
description: "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"  
author: "AVADHESH PATEL"  
published: 2012-07-13  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/322/textbox-validation-in-c-sharp-dot-net  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# TextBox Validation in C#.Net

##### TextBox Validation

In some cases I want to restricted that my [textbox](https://www.mindstick.com/articles/320/textbox-control-in-vb-dot-net) [accept](https://answers.mindstick.com/qa/50242/what-to-do-if-there-is-an-unreadable-language-on-the-logon-screen-that-will-probably-not-accept-your-username-and-password-is-there-any-bypass-or-just-a-removing-technique) only specific [input](https://www.mindstick.com/forum/159209/how-can-i-read-convert-an-input-stream-into-a-string-in-java) which we want. That means in [name](https://yourviews.mindstick.com/view/87450/how-to-generate-a-brand-name-using-linkedin-comprehensive-guide)’s textbox no one can enter special and numeric etc [character](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character). Like this salary’s textbox [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) entered only numeric [values](https://www.mindstick.com/forum/327/sum-textbox-values).

##### Character Validation

In this case user entered only character in texbox with backspace and [space](https://www.mindstick.com/articles/12954/do-your-electrical-repair-in-your-own-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](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net).

```
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](https://www.mindstick.com/forum/34709/please-write-a-program-for-decimal-to-binary-conversion-in-c-sharp) 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);        }
```

---

Original Source: https://www.mindstick.com/blog/322/textbox-validation-in-c-sharp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
