Date time picker validations

后端 未结 5 1171
死守一世寂寞
死守一世寂寞 2020-12-29 16:23

I am sorry for posting this question as it may be find silly to all, but I am not getting the exact solution.

The question is: I have a Date time picker in my proje

相关标签:
5条回答
  • 2020-12-29 16:44
    if (string.IsNullOrEmpty(dateInsert.Text)
              {
                MessageBox.Show("Please select date!");
                dateInsert.Focus();
                return;
               }
    

    hope this helps someone

    0 讨论(0)
  • 2020-12-29 16:45

    if you are using visual studio.....use this code to validate empty textbox

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="None"
            ErrorMessage="Select Date" ControlToValidate="dateInsert" ValidationGroup="validation"> </asp:RequiredFieldValidator>
    
    0 讨论(0)
  • 2020-12-29 16:54

    Please correct the code and see if it works

                   if (dateInsert.Value.ToString() == "")
                  {
                    MessageBox.Show("Please select date!");
                    dateInsert.Focus();
                    return;
                   }
    
    0 讨论(0)
  • 2020-12-29 16:57

    There is no direct solution to empty DateTimePicker. Only way to empty DateTimePicker is to set CustomFormat and then set empty space as value.

    dateTimePicker1.Format = DateTimePickerFormat.Custom;
    dateTimePicker1.CustomFormat = " ";
    

    Even if you do it value will be cleared in the control but if you access the value property of the control in code it will return the current date time. So your condition will always be false.

    //This is always false
    dateInsert.Value.ToString() = string.Empty
    

    SOLUTION

    Instead of using Value use Textin the condition.

    if(dateInsert.Text = " ")
    
    0 讨论(0)
  • 2020-12-29 17:11
    if(datepicker.Text == " ")
    messagebox.show("Please Select Date");
    
    // this works 100 %
    
    0 讨论(0)
提交回复
热议问题