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

  • Home
  • SEARCH
  • 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 8845859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:50:05+00:00 2026-06-14T11:50:05+00:00

I am trying to validate multiple fields/Controls based on individual conditions. The issue is

  • 0

I am trying to validate multiple fields/Controls based on individual conditions. The issue is I have individual labels that indicate an error based on the specific control rather than a message box; is there a more efficient way to do this?

Here is the code:

if (txtPhone. Text. Length <= 0) 
{ 
lblPhoneRequired.Visible = true; 
} 
else 
{ 
lblPhoneRequired.Visible = false; 
}

if (txtName. Text. Length <= 0) 
{ 
lblNameRequired. Visible = true; 
} 
else 
{ 
lblNameRequired. Visible = false; 
}

Finally I tried wrapping this into a Public method like this:

public void validation() {
if (txtPhone. Text. Length <= 0)
{ 
lblPhoneRequired.Visible = true; 
} 
else 
{ 
lblPhoneRequired.Visible = false; 
}

if (txtName. Text. Length <= 0) 
{ 
lblNameRequired. Visible = true; 
} 
else 
{ 
lblNameRequired. Visible = false; 
}
}

Then calling the method in a button click event but it does not work.

private void btnSample_Click(object sender, EventArgs e) 
validation(); 
}

The is new territory so please bear with my ignorance:

Guy

  • 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-06-14T11:50:06+00:00Added an answer on June 14, 2026 at 11:50 am

    You could shorten your code:

    lblPhoneRequired.Visible = (txtPhone.Text.Length == 0); 
    lblNameRequired.Visible =  (txtName.Text.Length  == 0); 
    

    Note that this is not more or less efficient than your appoach, but it is less verbose and maybe more readable.

    Of course you could also use a single control with a different error message:

    lblError.Visible =   (txtPhone.Text.Length == 0)
                      || (txtName.Text.Length  == 0);
    if((txtPhone.Text.Length == 0) && (txtName.Text.Length  == 0))
         lblError.Text = "Enter phone number and name";
    else if(txtPhone.Text.Length == 0)
         lblError.Text = "Enter phone number";
    else if(txtName.Text.Length == 0)
         lblError.Text = "Enter name";
    else
        lblError.Text = String.Empty;
    

    But you should have a look at the Validating event and the CausesValidation property which every control has.

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

Sidebar

Related Questions

I am trying to get jquery validate to work on multiple fields. Reason being
I am trying to validate multiple date fields similar to how it is being
I am trying to validate fields so user can't have entered data with empty
I have an XML file that I'm trying to validate against an XSD file
When trying to validate my site, I get the following error: Line 188, column
While trying to validate my forms i get the following error: Expected a {
While trying to validate form data on my page i get the following error:
I am trying to validate the entry in a cell based on the contents
I'm trying to validate an ID. I have this class called ManejadorTickets in which
I am using CraueFormFlowBundle to have a multiple page form, and am trying to

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.