Click here to Skip to main content
12,696,493 members (30,015 online)
Rate this:
 
Please Sign up or sign in to vote.
function validate_date(source, args) {
            var joinDate = document.getElementById('<%=txt_dateofjoin.ClientID %>').value;
            var confDate = document.getElementById('<%=txt_confirmationdate.ClientID %>').value;

            var dt = new Date(joinDate);
            var dt2 = new Date(confDate);
            var myDate = dt.format("dd-mm-yyyy");
            var myDate2 = dt2.format("dd-mm-yyyy");
            if (myDate < myDate2)
                args.IsValid = true;
            else
                args.IsValid = false;
        }

this code is not working. There are problem with "dt.format() function" I know it but how can I validate two dates now.
Posted 19-Aug-12 22:08pm
Updated 20-Aug-12 0:08am
v2
Comments
@amitgajjar 20-Aug-12 4:21am
   
are you getting any error ? or what is your problem ?
aspnet_regiis 20-Aug-12 4:56am
   
You have written this code and asking us what's wrong with it ??.. I think there's nothing wrong with the code, but with the person who has written it..
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

With the very less information you have posted , I am posting my most probable guess

if (myDate < myDate2)
                args.IsValid = true;
            else
                args.IsValid = false;




Date of join should be less than the confirmation date. Only in that case the .IsValid is true

NOTE: Instead of using javascript, you can use the CompareValidator in asp.net
  Permalink  
v2
Comments
Dinesh Ambaliya 20-Aug-12 5:09am
   
I tried CompareValidator but it doesn't work with "dd-mm-yyyy" format.
aspnet_regiis 20-Aug-12 5:18am
   
Did not work ? Thats impossible... You were not able to implement for this date format..

Give each a CompareValidator with Operator=DataTypeCheck and Type=Date to determine illegal dates

Add another CompareValidator to compare the two. Use ControlToValidate for startdate, ControlToCompare for enddate, Operator=LessThanEqual, and Type=Date

Now for the format of "dd/MM/yyyy". The validators use the current CultureInfo object of the thread to determine the date format. (CultureInfo.DateTimeInfo.ShortDatePattern specifically).

Set up the page to use the desired culture with <%@ Page culture="your culture" %>. Go to the .net docs topic for the CultureInfo class overview to see a list of cultures.
aspnet_regiis 20-Aug-12 5:19am
   
Was your javascript problem solved ?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Alternative way to fullfill your rquirement is Use CompareValidator in following way...Make appropriate changes in value of properties in given tag

<asp:CompareValidator  ID="CompareValidator1" runat="server"
 ErrorMessage="Confirm Date must be greater than Joining Date." ControlToCompare="TextBoxConfirmDate"
 ControlToValidate="TextBoxConfirmDate" ValueToCompare="TextBoxJoinDate"
 Operator="GreaterThan" Type="Date" ValidationGroup="GroupName"></asp:CompareValidator>
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Why do you want to format and compare? Both are dates. So you no need to format before doing compare

try this.
function validate_date(source, args) {
if(Date.Parse(document.getElementById('<%=txt_dateofjoin.ClientID %>').value<Date.Parse(document.getElementById('<%=txt_confirmationdate.ClientID %>').value)
args.IsValid=true;
else
args.IsValid=false;
  Permalink  
v3
Comments
Dinesh Ambaliya 20-Aug-12 9:37am
   
It's worked
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Try this
var dt = new Date(joinDate);
            var dt2 = new Date(confDate);
            var myDate = dt.getDate()
            var myDate2 = dt2.getDate();
            if (myDate < myDate2)
                args.IsValid = true;
            else
                args.IsValid = false;

Refer this link
http://srikanthtechnologies.com/blog/datecompare.html[^]
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170118.1 | Last Updated 20 Aug 2012
Copyright © CodeProject, 1999-2017
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