Link to home
Start Free TrialLog in
Avatar of newjeep19
newjeep19Flag for United States of America

asked on

C# textbox validation

IS there away using C# code to validate a textbox in C# and not ASP.NET that requiers text to be entered in.
Avatar of kaufmed
kaufmed
Flag of United States of America image

IS there away using C# code to validate a textbox in C# and not ASP.NET that requiers text to be entered in.
Can you clarify what you mean? Is this or is this not an ASP.NET application? If so, are you asking how to validate without using one of the FieldValidators, or are you asking which FieldValidator to use to validate an empty TextBox? If this is not an ASP.NET application, are you asking how to validate fields in a Forms application?
Avatar of newjeep19

ASKER

It is an ASP.NET application.  I want to know if it is possible to validate a text box in C# instead of ASP.NET.
 
The reason is because in my page load I have the following code:
protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["OpportunityId"] != null && Request["OpportunityId"] != "" && Request["type"] != null && Request["type"] != "")
            {
                if (!IsPostBack)
                {

                    //SHOW APPROVE OR REJECT CONTROLS
                    if (Request["type"] == "approve")
                    {
                        Label2.Text = "Approval reason: Optional";
                        Label2.Visible = true;

                        TextBox1.Visible = true;
                        TextBox1.MaxLength = 4000;

                        Button1.Visible = true;

                        Label1.Text = "Please select the approval button below.";

                        RadioButtonList1.Visible = true;
                        RadioButtonList1.SelectedValue = "Approved";
                    }
                    else if (Request["type"] == "reject")
                    {
                        Label2.Text = "Rejection reason: Requiered";
                        Label2.Visible = true;

                        TextBox1.Visible = true;
                        TextBox1.MaxLength = 4000;

                        RadioButtonList1.Visible = true;
                        RadioButtonList1.SelectedValue = "Reject";

                        Button1.Visible = true;

                        Label1.Text = "Please select the rejection button below.";
                    }
                }
            }
        }
in the type ==reject i would like to make the textbox requiered
Sure, you can do that. You're currently checking for !IsPostBack, so you can add an "else" to that condition. Inside the "else", you can do the appropriate checking. For example, you could check that the user entered something:

if (!IsPostBack)
{
   ...   // removed for brevity
}
else
{
    if (TextBox1.Text.Trim().Length <= 0)
    {
        // No valid text entered, alert user
        Label2.Text = "Please enter a reason for the rejection";
    }
    else    
    {
        Response.Redirect("newPage.aspx");
    }
}

Open in new window


The only thing I can't recall is that you are setting the TextBox visible in a non-postback scenario. If you have the TextBox not visible by default, I believe it will NOT be visible on the PostBack. You might need to set it visible in the !PostBack block also.
When I click on the submit button without entering in test after inserting the code above the textbox still is not requierd before I press the submit butt
is not requierd before I press the submit butt
For that, you're going to need javascript on the client side. What we were doing above was server-side validation. It's generally a better idea to do validation on the server, as a malicious user could easily turn off javascript or craft a malicious script to circumvent your client-side script.

Even though the submit button is pressed, the page shouldn't redirect because, by the logic above, that should only happen when there is text in the TextBox.
The page still redirects when there is no text in the textbox
Please wait for my solution I am preparing aa solution for you soon by tomorrow..
Try this method :
public static bool CheckValidation(TextBox tb)
{
      bool result = false;
      if(tb.Text.Trim()=="")
      {
             result = true;
      }
      return result;  
}

Open in new window

How to use it well use it like this :
if (!Page.IsPostBack)
{
    //1st time Load Logic..
}
else
{
    if (CheckValidation(TextBox1))
    {
        // No valid text entered, alert user
        Label2.Text = "Please enter a reason for the rejection";
    }
    else    
    {
        Response.Redirect("newPage.aspx");
    }
}

Open in new window

Still does not work.....When i click on the submit button without putting in any text into the textbox when the radiobutton reject is selected the application still  runs.....it does not post back or throw the error
Please submit the enitre "Page.aspx.cs" File in zipped format I need to examine the code deeply.. :)
Correction: ASP.NET is a technology for developing web based applications. You can write ASP.NET apps in many different languages, of which, C# and VB.NET are examples.

Nevertheless, have you considered using the RequiredFieldValidator? http://asp.net-tutorials.com/validation/required-field-validator/

The standard ASP.NET validation controls can prevent the page from being submitted and display error messages if there are invalid inputs.
ASKER CERTIFIED SOLUTION
Avatar of newjeep19
newjeep19
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Because i found a better solution than what is provided above