Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6805609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:36:26+00:00 2026-05-26T19:36:26+00:00

Right now the code below tests for a blank text box. If it is

  • 0

Right now the code below tests for a blank text box. If it is blank it returns the error stated below the if statement. That works fine, but I also want it to check for white spaces. I have tried the following for the first one:

if (String.IsNullOrWhiteSpace(txtFirstName.Text))

It does not work though. I typed the word “Bike” into the text box, but I spelled it like “B ike” with a space to see if it would return the error message and it didn’t.

public partial class frmPersonnel : System.Web.UI.Page
{
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //lblError.Text = "";
        try
        {
            if (txtFirstName.Text == "")
            {
                txtFirstName.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Please enter first name<br />";
            } 

            if (txtLastName.Text == "")
            {
                txtLastName.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Please enter last name! <br />";
            }
            if (txtPayRate.Text == "")
            {
                txtPayRate.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Please enter pay rate! <br />";
            }
            if (txtStartDate.Text == "")
            {
                txtStartDate.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Please enter start date! <br />";
            }
            if (txtEndDate.Text == "")
            {
                txtEndDate.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Please enter end date! <br />";
            }

            DateTime dt1;
            DateTime dt2;

            dt1 = DateTime.Parse(txtStartDate.Text);
            dt2 = DateTime.Parse(txtEndDate.Text);


            if (DateTime.Compare(dt1, dt2) > 0)
            {
                txtStartDate.BackColor = System.Drawing.Color.Yellow;
                txtEndDate.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Start Date must not be greater than End Date! <br />";
            }

            else
            {
                Session["txtFirstName"] = txtFirstName.Text;
                Session["txtLastName"] = txtLastName.Text;
                Session["txtPayRate"] = txtPayRate.Text;
                Session["txtStartDate"] = txtStartDate.Text;
                Session["txtEndDate"] = txtEndDate.Text;
                Server.Transfer("frmPersonalVerified.aspx");
            }
        }
        catch (Exception)
        {

        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:36:27+00:00Added an answer on May 26, 2026 at 7:36 pm

    I would change the following type of test:

    if (txtFirstName.Text == "")
    

    To:

    if (string.IsNullOrWhiteSpace(txtFirstName.Text)) // .NET 4.0+
    
    if (string.IsNullOrEmpty(txtFirstName.Text)) // .NET before 4.0
    

    And for your additional test (no spaces allowed in the string):

    if (string.IsNullOrWhiteSpace(txtFirstName.Text) && !txtFirstName.Text.Contains(" ")) // .NET 4.0+
    
    if (string.IsNullOrEmpty(txtFirstName.Text) && !txtFirstName.Text.Contains(" ")) // .NET before 4.0
    

    Note:

    You will need to check that lblError.Text doesn’t contain anything in order to continue to the next page, as this is what holds your errors. I can only see the DateTime test, so even if any of your txt controls have failed the validation, you still transfer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now i have a line of code, in vb, that calls a text
Right now my code works as such: def method_a self.method_b ==> 'method_b' end def
I have some code right now that executes when a link is clicked. The
Right now, I have code that looks something like this: Private Sub ShowReport(ByVal reportName
I have an odd edge case right now in that a response code from
The below code works fine on IE7 but not in Safari(5.0.5). If possible, I
The code below is where I am right now, I've made it so I
right now the jsp code will create html like below <a href=xxxxxx> <img width=143
I have this code right now to get the row value from jquery grid..
right now i am simply ftping everything (all of my source code included) but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.