1

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?

1
  • 1
    why you didn't use ASP.NET validators control? Commented Oct 29, 2014 at 12:32

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.