Hey Guys,
I have implemented an AJAX Toolkit CalendarExtender. I have a method 'CheckDate' in javascript (followed a tut!) that checks to see if the user selected a date > DateTime.Now().
Well, I have a few other things to check but not sure how to do them in javascript, so I'm hoping someone here can help me. I mean, i can somewhat convert it but it'll be too mangled, I'd rather learn the right way. I'm attaching the code and any help will be appreciated.
1 protected string CheckDateErrors(string dateGiven) 2 { 3 // let's make sure that the date entered by user 4 // does not exceed the present date or goes back 5 // over a year. 6 // we need to try parse! 7 DateTime userDate = DateTime.Parse(dateGiven); 8 //DateTime userDate; 9 10 int userYear = userDate.Year; 11 int userMonth = userDate.Month; 12 int userDay = userDate.Day; 13 14 // not valid! user entered future date, can't allow this to happen 15 if (userDate.CompareTo(DateTime.Now) > 0) 16 { 17 //this.tbBeginDate.ForeColor = Color.Red; 18 //this.tbBeginDate.BorderColor = Color.Red; 19 return "* Cannot Enter Future Dates;"; 20 } 21 // date cannot be more than one year old. 22 else if (userDate.CompareTo(DateTime.Now) <= 0) 23 { 24 int difference = DateTime.Today.Year - userYear; 25 26 // going too far back; now allowed. 27 if (userYear < (DateTime.Today.Year - 1)) 28 { 29 return "* Cannot Enter Past One Year: Compared Years;"; 30 } 31 else if ((difference == 1) && 32 (userMonth < DateTime.Today.Month)) 33 { 34 return "* Cannot Enter Past One Year: Compared Months;"; 35 } 36 else if ((difference == 1) && 37 (userMonth >= DateTime.Today.Month) && 38 (userDay < DateTime.Today.Day)) 39 { 40 return "* Cannot Enter Past One Year: Compared Days"; 41 } 42 43 return "none"; 44 45 } 46 47 return "none"; 48 49 }
Carpe Diem!
![]() |
0 |
![]() |
Hi
In order to validate date in javascript , you need to create a two date object's in javascript which will represent the current date and the date which you have selected by using Calander control .
Now following are the ways with which you can create a date object in javascript:-
Date() - Use the current date and time to create an instance of the object date.
Date(dateString) - Use the date specified by the string to create the instance of the date object. String format is "month day, year hours:minutes:seconds".
Date(year, month, day) - Create an instance of date with the specified values. Year is 0 to 99.
Date(year, month, day, hours, minutes, seconds) - Create an instance of date with the specified values.I have not modified your code but I am just giving few lines from my code which you can use to validate the date . Remember you will have to put some logic from your side in the code which I have mentioned below :-
// Function to validate dates before processing
function ValidateDate()
{
var returnValue = 0;
// Todays date
var currentDate = new Date();
// Array decleration
var arrayMonths = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var tempdate = new Date();
// Temporary startdate object
var tempStartDate = <Entered Date or date selected from calander control>; // I am using the following format dd-MMM-YYYY , if you are using different format then you need to modify the next three lines to get extarct year , day , monthvar startYear = tempStartDate.substring(tempStartDate.lastIndexOf('-')+1); // get the year
var startDay = tempStartDate.substring(0 , tempStartDate.indexOf('-')); // get the day
var startMonth = tempStartDate.substring(tempStartDate.indexOf('-') + 1 , tempStartDate.lastIndexOf('-')); // get the month
// Start date
var startDate = new Date(startYear , arrayMonths.indexOf(startMonth), startDay); // Create an instance of date object in a format which javascript understands( javascript representaion of selected date)// After this you have to implement the logic of date validation
// For this you can make use of various javascript method like getYear() , getDay() etc on date object to extract relevant information
// Also once you have instantiated date in the form of javascript object you can use arithmatic operators like < , > , >= , <= , === , !== etc}
And while you are validating your selected date following link will be of great help to you :- http://www.comptechdoc.org/independent/web/cgi/javamanual/javadate.html
Also there can be various ways by which you can validate date , the above approach may seems more complex and cumbersome but I learned a lot from the above approach . If you can get some more efficient method please provide the same in current thread .
Regards
Pawan Mishra
“The greatest compliment that was ever paid to me was when one asked me what I thought, and attended to my answer.” - Henry David Thoreau
![]() |
0 |
![]() |
Pawan,
thanks a LOT for the help. I've been using your help as well as the link to convert my method to a javascript function. I am stuck on one part though.
1 else if(sender._selectedDate <= currentDate) 2 { 3 //var userDate = (Date)(sender._selectedDate); 4 var yearDay = usersender._selectedDate.getDate(); 5 var userMonth = sender._selectedDate.getMonth(); 6 var userYear = sender._selectedDate.getYear(); 7 8 int difference = currentYear - userYear; 9 10 // going too far back; now allowed. 11 if (userYear < (currentYear - 1)) 12 { 13 alert("Error: No Dates Past A Year Allowed"); 14 }The problem is located in lines 3-6. I need to extract the month, year, and date from sender._selectedDate. I'm not sure how to do this part in JS. Can anyone help me, please? Thanks.
Carpe Diem!
![]() |
0 |
![]() |
Hi
getDate() , getMonth() etc kind of functions will work only with date whose format is actually compatible with javascript date format . For eg in your javascript function give :-
var date = new Date();
alert(date); --> result will be something like this " Tue Dec 9 smthn smthn " .
Since in my application the date format of calander extender control which i was using was dd-MMM-YYYY so i had to maually write the code to extract the specific information which is day , month and year and then pass these details into the constructor of date class of javascript .
If you can tell me which format you are using in you application I can then help you in getting through your problem.
Regards
Pawan Mishra
“The greatest compliment that was ever paid to me was when one asked me what I thought, and attended to my answer.” - Henry David Thoreau
![]() |
0 |
![]() |
Hi Pawan,
Thanks a lot for your help. You've been extremely helpful. My date format is MM/dd/yyyy. Looking at your example code, which somehow I missed!, I believe I know what to do. I will update this post in a little bit. You rock!
Carpe Diem!
![]() |
0 |
![]() |
I'm still having issues and have a few questions. I will first paste the code:
1 var userDate = sender._selectedDate; 2 alert("userDate: " + userDate); 3 4 var hi = userDate.substring(1, 3); 5 alert("hi!"); 6 var userYear = userDate.substring(userDate.lastIndexOf('/')+1); 7 alert("userYear/lastIndexOf: " + userDate.substring(userDate.lastIndexOf('/')+1)); 8 9 var userMonth = userDate.substring(0 , userDate.indexOf('/')); 10 var userDay = userDate.substring(userDate.indexOf('/') + 1 , userDate.lastIndexOf('/')); 11 alert("MM/dd/yyyy: " + userMonth + "/" + userDay + "/" + userYear);Ok, first regarding what format I'm using. This is the exact code in designer:
a <cc1:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="tbEndDate" b PopupButtonID="Image2" OnClientDateSelectionChanged="checkDate" Format="MM/dd/yyyy" />Lines 1 & 2 execute. The alert box shows me this format "Thu Dec 4 00:00:00 EST 2008". Should it not show me 12/04/2008..? Have I confused something here? Lines 4 & 5 were added to see wth was going on since I kept getting the error in IE "Object does not support this method or property". Since line 6 uses substring and indexOf, I had to determine what method isn't working properly. Turns out line 4 is where the code craps out.
Pawan or anyone else, can anyone help me out here? I am using IE 7 (not by choice, but this is what 99% of our users use here).
Carpe Diem!
![]() |
0 |
![]() |
Hi
You can check the following code , I have written to modify to validate of format MM/dd/YYYY :-
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="TextChanged.aspx.cs" Inherits="Admin_TextChanged" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" ><
head runat="server"> <title>Temp1</title> <script language="javascript" type="text/javascript"> // Function to validate dates before processingfunction ValidateDate(){
var returnValue = 0;// Todays datevar currentDate = new Date();
// Temporary startdate objectvar tempStartDate = $get('txtCal').value; var toYear = tempStartDate.substring(tempStartDate.lastIndexOf('/')+1);var toMonth = tempStartDate.substring(0 , tempStartDate.indexOf('/')); var toDay = tempStartDate.substring(tempStartDate.indexOf('/') + 1 , tempStartDate.lastIndexOf('/')); // Start datevar startDate = new Date(toYear , toMonth - 1 , toDay);
if(startDate > currentDate)
{
alert(" Start date cannot be greater than todays date ");}
// Here you can carry on your validation I have just validated one scenario where selected date should not be greated than todays date}
</script> </head><
body> <form id="form1" runat="server"> <div> <ajax:ScriptManager ID="script1" runat="server" EnablePartialRendering="true"></ajax:ScriptManager> <asp:Button runat="server" ID="btnClick" Text="Button" OnClientClick="ValidateDate()" /> <asp:TextBox runat="server" ID="txtCal"></asp:TextBox> <ajaxToolkit:CalendarExtender BehaviorID="Cal1" ID="Cal" TargetControlID="txtCal" runat="server" Format="MM/dd/yyyy"></ajaxToolkit:CalendarExtender> </div></form> </body></
html>
Let me know if you need some more help :-) ....
Regards
Pawan Mishra
“The greatest compliment that was ever paid to me was when one asked me what I thought, and attended to my answer.” - Henry David Thoreau
![]() |
0 |
![]() |
Pawan,
var tempStartDate = $get('txtCal').value;
does the trick! Great. Last question:
sender._textbox.set_Value(sender._selectedDate.format(sender._format))
The above line.... what if I want to leave the textbox blank? I tried '' and ' ', but got an error. Got ideas?
Carpe Diem!
![]() |
0 |
![]() |