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
3 Answers
Reset to default 4just 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
评论列表(0条)