When you are building any form where user have to enter some date, then you may have requirement to allow only future dates / past dates.
So here i am going to explain how to check whether a user has entered date which is greater than today's date or less than today's date.
Conside you have a TextBox to allow user to enter a date and a Button to submit the data.
Enter Date(in mm-dd-yyyy format) : <input type="text" id="txtdate" />
<input value="SUBMIT" id="btnsubmit" onclick="checkDate();"/>
The javascript/ JQuery code to validate the date is
function checkDate() {
var EnteredDate = document.getElementById("txtdate").value; //for javascript
var EnteredDate = $("#txtdate").val(); // For JQuery
var date = EnteredDate.substring(0, 2);
var month = EnteredDate.substring(3, 5);
var year = EnteredDate.substring(6, 10);
var myDate = new Date(year, month - 1, date);
var today = new Date();
if (myDate > today) {
alert("Entered date is greater than today's date ");
}
else {
alert("Entered date is less than today's date ");
}
}
In this way you can check wether the entered date is Greater/Less than the current date.
Hope this post helps you.
For more posts regarding JavaScript/JQuery See this: Javascript/JQuery
So here i am going to explain how to check whether a user has entered date which is greater than today's date or less than today's date.
Conside you have a TextBox to allow user to enter a date and a Button to submit the data.
Enter Date(in mm-dd-yyyy format) : <input type="text" id="txtdate" />
<input value="SUBMIT" id="btnsubmit" onclick="checkDate();"/>
The javascript/ JQuery code to validate the date is
function checkDate() {
var EnteredDate = document.getElementById("txtdate").value; //for javascript
var EnteredDate = $("#txtdate").val(); // For JQuery
var date = EnteredDate.substring(0, 2);
var month = EnteredDate.substring(3, 5);
var year = EnteredDate.substring(6, 10);
var myDate = new Date(year, month - 1, date);
var today = new Date();
if (myDate > today) {
alert("Entered date is greater than today's date ");
}
else {
alert("Entered date is less than today's date ");
}
}
In this way you can check wether the entered date is Greater/Less than the current date.
Hope this post helps you.
For more posts regarding JavaScript/JQuery See this: Javascript/JQuery