2014-10-29 87 views
1

我对验证空文本框问题校验空值

我的文本框

<asp:TextBox ID="TextBox1" runat="server" MaxLength="50" Width="272px" AutoCompleteType="Disabled"> 

我的标签

<asp:Label ID="warning" runat="server" Text="you forgot about this" ForeColor="Red" Visible="false"></asp:Label> 

我的验证

if (TextBox1.Text == "") 
      { 
       warning.Visible = true; 
      } 

它可以验证空的文本框,但它不能验证空间输入

有人可以帮我吗?

+1

为什么你没有使用ASP.NET验证控件? – 2014-10-29 12:32:15

回答

2

这是因为空间不是"",所以它们不相等。

您可以使用.IsNullOrWhiteSpace代替:

if (string.IsNullOrWhiteSpace(TextBox1.Text)) 

这也检查null(尽管在这种特殊情况下,我不认为.Text将永远是null)以及其他任何纯粹的好处空白字符。