Hudhaifa Yoosuf
Hudhaifa Yoosuf

Reputation: 919

Check If Valid Jquery Date

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");
  }

}

Upvotes: 2

Views: 4442

Answers (3)

KiRa
KiRa

Reputation: 924

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.

Upvotes: 1

Asifuzzaman
Asifuzzaman

Reputation: 1783

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"/>

Upvotes: 0

Beginner
Beginner

Reputation: 4153

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");
  }

}

Upvotes: 4

Related Questions