javascript - Check If Valid Jquery Date - Stack Overflow

I want to check if the user has selected a valid date. When i call getDateFilteredComments() method.I

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

}

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

}
Share Improve this question asked Sep 21, 2016 at 3:34 Hudhaifa YoosufHudhaifa Yoosuf 9192 gold badges13 silver badges28 bronze badges 9
  • Why wouldn't the user select a valid date, what other options are there with a datepicker ? – adeneo Commented Sep 21, 2016 at 3:37
  • @adeneo user can type random text as well,so i need to restrict that – Hudhaifa Yoosuf Commented Sep 21, 2016 at 3:38
  • <input type="date" required /> – adeneo 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 – Hudhaifa Yoosuf Commented Sep 21, 2016 at 3:41
  • 1 You only need a date that pick from a datapicker not a random input right? – KiRa Commented Sep 21, 2016 at 7:47
 |  Show 4 more ments

3 Answers 3

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

}

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.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745197729a4616174.html

相关推荐

  • javascript - Check If Valid Jquery Date - Stack Overflow

    I want to check if the user has selected a valid date. When i call getDateFilteredComments() method.I

    20小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信