Click here to Skip to main content
15,886,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Team ,

I have a simple project having textbox and button

I want to show that If my textbox having specific text like " This is codeproject " then on

clicking button , it should show "Welcome"

otherwise it should show error.

please tell me how to do it.

-Rectus
Posted
Comments
Nandakishore G N 25-Jun-14 9:46am    
Paste, what have you done till now?

For this validation, a if-else statement is enough. Create a label in which you'll show "Welcome" or an error. When adding the label to your web form, it should be empty. Then, add the following code to your button click method:
C#
// in your button click method:
if (yourTextBox.Text.Equals("This is codeproject", StringComparison.Ordinal)) // if you want a case-insensitive comparison, use OrdinalIgnoreCase
{
    yourLabel.Text = "Welcome";
}
else
{
    yourLabel.Text = "Error";
}

However, for more advanced validation techniques (like only allow values between a specific range, validating a required field ...), it is better to use the ASP.NET Validation Controls:
http://msdn.microsoft.com/en-us/library/debza5t0%28v=vs.100%29.aspx[^]
 
Share this answer
 
v2
Comments
Member 10272175 25-Jun-14 9:56am    
Thanks for ans
Sir I have long text which I have to validate , so How can I put this long text ?
Thomas Daniels 26-Jun-14 2:15am    
Just use the same way, but use another string instead of This is codeproject.
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "This is CodeProject")
        {
            Response.Write("Welcome");//your mesg
        }
        else
        {
            Response.Write("ERROR"); msg
        }
    }
 
Share this answer
 
Exactly how you do this depends on the exact environment you are working in: for a website, I would probably do it in JavaScript as it would be a lot more responsive than doing the check at the server.

But...you can do it in C# (and it will work in web or windows) by handling the Click event for the button, and checking the Text property of the textbox:
C#
if (myTextBox.Text == "This is Codeproject")
   {
   // Display your "welcome" message
   ...
   }
else
   {
   // Show error message
   ...
   }


We can't tell you how to display it: we don't know enough about your system.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900