2

I want to check if the user has selected a valid date. When i call getDateFilteredComments() method. I need to capture that in the IF condition.

 $("#StartDate").datepicker({
  dateFormat: "d M y"
});

function getDateFilteredComments() {

  if (Condition) {
    alert("Valid");
  } else {
    alert("Not Valid");
  }

}
9
  • Why wouldn't the user select a valid date, what other options are there with a datepicker ? Commented Sep 21, 2016 at 3:37
  • @adeneo user can type random text as well,so i need to restrict that Commented Sep 21, 2016 at 3:38
  • <input type="date" required /> Commented Sep 21, 2016 at 3:39
  • @adeneo this wont help, can i restrict the user from entering a date, but he should be able to select it Commented Sep 21, 2016 at 3:41
  • 1
    You only need a date that pick from a datapicker not a random input right? Commented Sep 21, 2016 at 7:47

3 Answers 3

4

just convert your string to date using new Date(dateString) also change your date format I don't think that's valid

function getDateFilteredComments() {

  if (new Date($("#StartDate").val()) != "Invalid Date") {
    alert("Valid");
  } else {
    alert("Not Valid");
  }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Couldn't use the .val() but I was able to use .toString() and the same logic is applied (i.e. Invalid Date is still returned if fails to convert). Not sure if it changed, or if my constructor was different.
1

If you want to restrict the user to input anything and accept Date from the data picker try this one JSFIDDLE.

Hope it will help you.

1 Comment

Glad to help you friend.
0

you can try this

<script type="text/javascript">

 function fu(){

 var text = document.getElementById("it").value;
 var take = text.split('/');
  var mo = parseInt(take[0], 10);
 var da = parseInt(take[1], 10);
  var ye = parseInt(take[2], 10);
 var date = new Date(ye,mo-1,da);
 if (date.getFullYear() == ye && date.getMonth() + 1 == mo && date.getDate()
   ==  da) {
   alert('Valid date input');
    } else {
    alert('Invalid date input');
  }
   }
</script>


<input type="text" id="it" placeholder="Add date..."/>

<input type="submit" id="ad" onclick="fu()" value="testd"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.