---
title: "How to require text in a Textbox?"  
description: "How to require text in a Textbox?"  
author: "Allen Scott"  
published: 2014-11-25  
updated: 2014-11-26  
canonical: https://www.mindstick.com/forum/12688/how-to-require-text-in-a-textbox  
category: "asp.net"  
tags: ["asp.net mvc"]  
reading_time: 1 minute  

---

# How to require text in a Textbox?

How do I go about requiring [text](https://www.mindstick.com/blog/301635/did-people-reinvent-texting-to-express-the-full-range-of-emotions) in a [textbox](https://www.mindstick.com/forum/12714/dynamic-textbox-in-gridview)? This is what i have so far.

```
String strName = txtName.Text;String strEmail = txtEmail.Text;Boolean blnErrors = false; if (strName == null){ }else{    string script ="alert(\"Name Field Is Required!\");";   
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", script, true);     txtName.Focus();}
```

When I run the [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) and try to [execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) it, I get the [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) [popping up](https://answers.mindstick.com/qa/51481/how-to-fix-an-iphone-se-problem-on-apple-id-verification-prompt-that-keeps-popping-up) regardless if I have text entered into the textbox or not. I only want the Error to show if There is Nothing in the TextBox. I have also tried using,

if (strName == "")

as well. But nothing changes.

## Replies

### Reply by Anonymous User

```
String strName = txtName.Text.Trim(); //add trim hereString strEmail = txtEmail.Text;Boolean blnErrors = false; if (string.IsNullOrWhiteSpace(sstrName)) //this function
checks for both null or empty string.{    string script ="alert(\"Name Field Is Required!\");";   
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", script, true);    txtName.Focus();    return;//return
from the function as there is an error.}
```


---

Original Source: https://www.mindstick.com/forum/12688/how-to-require-text-in-a-textbox

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
