Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
I have been trying out for a code where there is a control to enter a date and the entered date has to be validated such that entered date must be earlier than or equal to current date.
Can anyone please help me achieve this?
Posted
Updated 27-Mar-13 22:44pm
v2
Comments
Ankur\m/ 28-Mar-13 5:27am    
Did you search Google for calender control asp.net? AJAX calendar control is used a lot. add jquery to your search string and there will be lot of results you will be interested in.

Your question is not clear...
I suggest you to see this: http://www.w3schools.com/js/js_obj_date.asp[^]
 
Share this answer
 
Hi,

Hope you are entering your date in dd/MM/yyyy format. So here is the sample code for the solution to your problem.

Place a TextBox in your .aspx page like this.

ASP.NET
<asp:TextBox ID="txtDate" runat="server" Width="100px" onblur="checkDate(this.value);"></asp:TextBox>


Place the below script in your .aspx page

JavaScript
<script type="text/javascript" language="javascript">

    function checkDate(input) {
    
    var today = new Date();
    var todayFormat = today.format("MM/dd/yyyy");
    var endDate = formatDate(input);
    if (endDate > todayFormat) {
        alert("Entered date must be earlier than or equal to current date!");
    }
}

function formatDate(input) {
            var datePart = input.match(/\d+/g);
            var day = datePart[0] 
            var month = datePart[1]
            var year = datePart[2];
            var myDate = month + '/' + day + '/' + year;
            return myDate;
        }
</script>


Hope this will solve your problem. Please let me know if you face any issues.

Regards,
Manoj
 
Share this answer
 
Comments
codegirl24 8-Apr-13 4:00am    
Manoj :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script type="text/javascript">
function formValidation(){
var dateString=document.form1.date.value;
if(checkDate(dateString)){
}
return false;
}
function checkDate(dateString) {

var today = new Date();
var todayFormat = today.format("MM/dd/yyyy");
var endDate = formatDate(dateString);
if (endDate > todayFormat) {
alert("Entered date must be earlier than or equal to current date!");
}
}

function formatDate(dateString) {
var datePart = dateString.match(/\d+/g);
var day = datePart[0]
var month = datePart[1]
var year = datePart[2];
var myDate = month + '/' + day + '/' + year;
return myDate;
}

</script>

</HEAD>
<BODY>
<form name ="form1" action="">
*Date(mm/dd/yyyy)<br><input type="text" name="date">
<input type="button" value="Done" önclick="formValidation();">
</BODY>
</BODY>
</HTML>
--------------------------------
This is my code Can you please tell me where I have gone wrong?
Sorry for replying late...Thanks a lot for the solution.
Heres my code and it works :)
It is definitely trivial kind of code but it works :)
Thanks Manoj,Maciej Los for providing me the basic idea of how i must work for this.







<title> New Document





function checkDate(){
var input = document.form1.dateString.value;
var arr = input.split("/");
var newdate= new Date();
var sysday = newdate.getDate();
var sysmonth=newdate.getMonth();
var sysyear=newdate.getFullYear();
if((arr[0] &gt; sysday) || (arr[1] &gt; sysmonth) || (arr[2] &gt; sysyear)){
alert("wrong date");
}
}






Date




 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900