Reputation: 43
I have problem about validating empty textbox
My textbox
<asp:TextBox ID="TextBox1" runat="server" MaxLength="50" Width="272px" AutoCompleteType="Disabled">
My label
<asp:Label ID="warning" runat="server" Text="you forgot about this" ForeColor="Red" Visible="false"></asp:Label>
my validation
if (TextBox1.Text == "")
{
warning.Visible = true;
}
it can validate the empty textbox but it can't validate space input
can anybody help me please?
Upvotes: 1
Views: 13404
Reputation: 51
try to use RequiredFieldValidator it validates both client side and server side and also ignore whitespaces during validation. http://msdn.microsoft.com/ru-ru/library/system.web.ui.webcontrols.requiredfieldvalidator(v=vs.110).aspx
Upvotes: 0
Reputation: 218828
That's because a space isn't ""
, so they're not equal.
You can use .IsNullOrWhiteSpace
instead:
if (string.IsNullOrWhiteSpace(TextBox1.Text))
This has the added benefit of also checking for null
(though in this particular case I don't think .Text
would ever be null
) as well as any other purely whitespace characters.
Upvotes: 2