validate texbox using this code in c#

RAVI 1,056 Reputation points
Oct 10, 2024, 11:24 AM

Hello

I have one asp.net textbox and one asp.net button when user click i want to check if Textbox1 has value 11 or 22 or 33 then okay if any other number i want to show alert as "Wrong Data"

how to do so using asp.net c#

thanking you

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,560 questions
{count} votes

2 answers

Sort by: Most helpful
  1. SurferOnWww 3,696 Reputation points
    Oct 11, 2024, 12:48 AM

    I have one asp.net textbox and one asp.net button when user click i want to check if Textbox1 has value 11 or 22 or 33 then okay if any other number i want to show alert as "Wrong Data"

    Use the CustomeValidator control:

    CustomValidator Class

    There might be no other solution better than it.

    0 comments No comments

  2. Lan Huang-MSFT 30,086 Reputation points Microsoft Vendor
    Oct 11, 2024, 6:47 AM

    Hi @RAVI,

    If you only need to verify the three numbers 11, 22, and 33, it is very easy to do.

    protected void Button1_Click(object sender, EventArgs e)
    {
        string a = TextBox1.Text;         
        if(a != "11" && a!="22" && a != "33")
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Wrong Data');", true);
            TextBox1.Text = "";
        }           
    }
    

    If you want to verify the multiple of 11, as follows:

    protected void Button1_Click(object sender, EventArgs e)
     {
         string a = TextBox1.Text;                      
         if (int.TryParse(a, out int value))
         {
             bool isMultiple = value % 11 == 0;
             if (isMultiple == false)
             {
                 ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Wrong Data');", true);
                 TextBox1.Text = "";
             }
         }
         else
         {
             ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Wrong Data');", true);
             TextBox1.Text = "";
         }
     }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.