Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript
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 27-Mar-13 23:39pm
Edited 27-Mar-13 23:44pm
v2
Comments
Ankur\m/ at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Your question is not clear...
I suggest you to see this: http://www.w3schools.com/js/js_obj_date.asp[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:TextBox ID="txtDate" runat="server" Width="100px" onblur="checkDate(this.value);"></asp:TextBox>
 
Place the below script in your .aspx page
 
<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
  Permalink  
Comments
codegirl24 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Heres my code and it works Smile | :)
It is definitely trivial kind of code but it works Smile | :)
Thanks Manoj,Maciej Los for providing me the basic idea of how i must work for this.
 

 

 
<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 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] > sysday) || (arr[1] > sysmonth) || (arr[2] > sysyear)){
alert("wrong date");
}
}
 
</script>
</meta></meta></meta></meta></head>
 
<body>
<form name="form1" action="">
Date<input type="text" name="dateString">
<input type="button" value="Done" >
</input></input></form>
</body>
 
</html>
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 381
1 Sergey Alexandrovich Kryukov 245
2 Marcin Kozub 225
3 Praneet Nadkar 217
4 /\jmot 189
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 8 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100