php - Check if a date has been selected. Bootstrap input type date

680

I have some code where I have a simple Bootstrap datepicker<input type="date"/> I need to check whether a value has been selected using JavaScript. When I echo the current value of the input where the date hasn't been selected, I just get a blank and the condition under if statement always results to true. Can someone assist in how to check this correctly?

\EDIT/UPDATE I can successfully get the value of the datepicker when a date has been selected. I usevar date=$('#date').val() which works well when a date has been selected. But when a date hasn't been selected, it just returns a blank. I need to check for a situation where a date hasn't been selected and do something ... I have triedif(date="") andif(date=null) none of these are efficient. The function never jumps to the else. The if statement always results to true. Is there something I am doing wrong or is my approach totally out of question?

511

Answer

Solution:

As mentioned in comment, you should write

var selectedDate = $("#date").val();
if(selectedDate == "") {  // use == instead of = here. It will check condition
  alert("date is not selected");
}else{
  alert(selectedDate);
}

Note:- single= is an assignment operator. It assigns the value to variable. While== checks the condition. You can also write if(selectedDate) to check date is selected or not.

People are also looking for solutions to the problem: php - Remove an action based on screen size in WordPress / WooCommerce

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.