locked
How to enable validation on Textbox TextMode=Date RRS feed

  • Question

  • User-367318540 posted

    i want to enable validation 

      <asp:TextBox ID="txtitempbstart" runat="server" TextMode="Date"></asp:TextBox>

    when date is not selected?
     

    Friday, November 15, 2019 1:35 PM

Answers

  • User665608656 posted

    Hi akhterr,

    According to the code you provided, I found that you triggered the code behind event and verified it through another textbox's (TextBox1) OnTextChanged event.

    Because textbox's OnTextChanged event does not trigger form validation, the above two methods did not succeed.

    I suggest that you can directly judge and verify the TextBox control of date type in the OnTextChanged event. For details, please refer to the code below:

        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="txtitempbstart" runat="server" TextMode="Date" required></asp:TextBox><asp:Label ID="Label1" runat="server" Text="" ForeColor="Red"></asp:Label>
                <br />
                <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
            </div>
        </form>
           protected void TextBox1_TextChanged(object sender, EventArgs e)
            {
                Label1.Text = "";
                if (string.IsNullOrEmpty(txtitempbstart.Text))
                {
                    Label1.Text = "please select start date!";
                    TextBox1.Text = "";
                    return;
                }
    // seaerchbybaleno(); var xx = DateTime.Parse(txtitempbstart.Text).ToString("yyyy-MM-dd ", System.Globalization.CultureInfo.InvariantCulture); }

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 21, 2019 2:01 AM

All replies

  • User665608656 posted

    Hi akhterr,

    akhterr

    i want to enable validation 

      <asp:TextBox ID="txtitempbstart" runat="server" TextMode="Date"></asp:TextBox>

    when date is not selected?

    According to your requirements, if you want to add non-empty validation to textbox, you can implement it in many ways.

    For the simplest implementation, you can add ‘required’ attribute after the current TextBox control to indicate the necessity of this textbox.

        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="txtitempbstart" runat="server" TextMode="Date" required></asp:TextBox> 
              <br />  <input id="Submit1" type="submit" value="submit" />  </div>
        </form>

     

    Or you can add the server validation control RequiredFieldValidator to perform non-empty validation on textbox.

      <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="txtitempbstart" runat="server" TextMode="Date"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtitempbstart" ErrorMessage="please select a date!" Display="Dynamic" ForeColor="red"></asp:RequiredFieldValidator>
              <br />  <input id="Submit1" type="submit" value="submit" />  </div>
        </form>

    Best Regards,

    YongQing.

    Monday, November 18, 2019 5:42 AM
  • User-367318540 posted

    i used both validation as you share but my application getting crash 

    String was not recognized as a valid DateTime. Please view image link

    https://ibb.co/196Z8d4 

    Monday, November 18, 2019 6:55 AM
  • User665608656 posted

    Hi akhterr,

    For this issue , If you need to trigger this button's code behind event, I suggest you add the reuqired attribute to make it easier.

     <div>
                <asp:TextBox ID="txtitempbstart" runat="server" TextMode="Date" required></asp:TextBox>
               <br />
                <input id="Submit1" type="submit" value="submit" runat="server" onserverclick="Submit1_ServerClick"  />
            </div>
            protected void Submit1_ServerClick(object sender, EventArgs e)
            {
                var date = DateTime.Parse(txtitempbstart.Text).ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
            }

    Best Regards,

    YongQing.

    Monday, November 18, 2019 9:53 AM
  • User-367318540 posted

    I used your both code but still have same issue 

    here is my complete code

    protected void TextBox1_TextChanged(object sender, EventArgs e)
            {
                seaerchbybaleno();
            }
    
            private void seaerchbybaleno()
            {
                SqlCommand cmd = new SqlCommand("Searchbyordernou");
                var date = DateTime.Parse(txtitempbstart.Text).ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
    
                cmd.Parameters.Add(new SqlParameter("@StartDate", date));
                cmd.Parameters.Add(new SqlParameter("@EndDate", DateTime.Parse(txtitempbend.Text).ToString("yyyy-MM-dd ", System.Globalization.CultureInfo.InvariantCulture)));
    
    
                cmd.Parameters.Add(new SqlParameter("@orderno", TextBox1.Text));
    
    
                cmd.Connection = con;
    
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
    
                DataTable dt = new DataTable();
    
                da.Fill(dt);
                GVallbb.DataSource = dt;
                GVallbb.DataBind();
                con.Close();
            }
    
    

    Wednesday, November 20, 2019 4:01 AM
  • User665608656 posted

    Hi akhterr,

    According to the code you provided, I found that you triggered the code behind event and verified it through another textbox's (TextBox1) OnTextChanged event.

    Because textbox's OnTextChanged event does not trigger form validation, the above two methods did not succeed.

    I suggest that you can directly judge and verify the TextBox control of date type in the OnTextChanged event. For details, please refer to the code below:

        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="txtitempbstart" runat="server" TextMode="Date" required></asp:TextBox><asp:Label ID="Label1" runat="server" Text="" ForeColor="Red"></asp:Label>
                <br />
                <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
            </div>
        </form>
           protected void TextBox1_TextChanged(object sender, EventArgs e)
            {
                Label1.Text = "";
                if (string.IsNullOrEmpty(txtitempbstart.Text))
                {
                    Label1.Text = "please select start date!";
                    TextBox1.Text = "";
                    return;
                }
    // seaerchbybaleno(); var xx = DateTime.Parse(txtitempbstart.Text).ToString("yyyy-MM-dd ", System.Globalization.CultureInfo.InvariantCulture); }

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 21, 2019 2:01 AM