Click here to Skip to main content
16,017,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Start Date format is 23-Dec-2011(dd-MMM-yyyy)
End Date format is 28-Dec-2011(dd-MMM-yyyy)

I want to check validation in javascript(Client side) that is
Start date can not be less than end date.
But it not work properly.
Please help.
I am waiting for your response.

Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 19-Mar-11 1:43am    
Not only this is wrong approach, buy you don't show you bugs. Who will be interested to write correct code on your spec? Why?
But if you share your code, chances are you can get a fix.
--SA

C#
function CompareFirstFieldDateGreaterThanSecondFieldDate(ctrlIDFirst, ctrlIDSecound, ctrlNameFirst,ctrlNameSecound)
{
    var fieldDateFirst = document.getElementById(ctrlIDFirst).value ;
    var fieldDateSecound = document.getElementById(ctrlIDSecound).value ;

    if((fieldDateFirst == "")&&(fieldDateSecound == ""))
    {
        return true;
    }
    else
    {
        if(fieldDateFirst == "")
        {
            alert("Please enter " +ctrlNameFirst);
            //document.getElementById(ctrlIDFirst).value = ""; -- Commented By Perumal on 23 July 2010
            document.getElementById(ctrlIDFirst).focus();
            return false;
        }
        else if(fieldDateSecound == "")
        {
            alert("Please enter " +ctrlNameSecound);
           // document.getElementById(ctrlIDSecound).value = ""; -- Commented By Perumal on 23 July 2010
            document.getElementById(ctrlIDSecound).focus();
            return false;
        }
        else
        {
            fieldDateFirst = fieldDateFirst.split("/");
            var Date1 = new Date();
            Date1.setFullYear(fieldDateFirst[2],fieldDateFirst[1]-1,fieldDateFirst[0]);

            fieldDateSecound = fieldDateSecound.split("/");
            var Date2 = new Date();
            Date2.setFullYear(fieldDateSecound[2],fieldDateSecound[1]-1,fieldDateSecound[0]);

            if (Date1 > Date2)
            {
              return true;
            }
            else
            {
              //alert(" Please Enter "+ ctrlNameFirst + " and " + ctrlNameSecound +".");
              alert(ctrlNameFirst + " should be greater than " + ctrlNameSecound + ".");
              // document.getElementById(ctrlIDFirst).value = ""; -- Commented By Perumal on 23 July 2010
              document.getElementById(ctrlIDFirst).focus();
              return false;
            }
        }
    }
}

This works fine
 
Share this answer
 
v2
Comments
Anuja Pawar Indore 10-Feb-12 6:20am    
Added pre tag
Nilesh Patil Kolhapur 10-Feb-12 6:29am    
but i think compare validator is better
Arvind Chandrasekaran 10-Aug-12 8:06am    
its working fine
thanks ha'
Just use the Date object of Javascript to define the dates and then simply use the date object comparison.

Javascript Date object: http://www.w3schools.com/js/js_obj_date.asp[^]

Sample:
JavaScript
var myDate=new Date();
myDate.setFullYear(2010,0,14);
var today = new Date();
if (myDate>today)
{
  alert("Today is before 14th January 2010");
}
else
{
  alert("Today is after 14th January 2010");
}
 
Share this answer
 
Comments
Emon Mahmud 19-Mar-11 2:01am    
Thanks for your valuable information.
Hi,

My first suggestion is use compare validator its work super
set
ControlToValidate= txtToDate
ControlToCompare =txtFromDate
setfocusonerror=true
Operator =GreaterThan
Type=date


its work superbly

otherwise u use following javascript
JavaScript
function CheckValidDate(fromDate, toDate) {
    var chk = /^(((((0[1-9])|(1\d)|(2[0-8]))\/((0[1-9])|(1[0-2])))|((31\/((0[13578])|(1[02])))|((29|30)\/((0[1,3-9])|(1[0-2])))))\/((20[0-9][0-9])|(19[0-9][0-9])))|((29\/02\/(19|20)(([02468][048])|([13579][26]))))$/
    if (fromDate.value != "" && toDate.value != "" && toDate != 'T') {
        if (chk.test(fromDate.value) && chk.test(toDate.value)) {
            if (FormatDate(fromDate.value) > FormatDate(toDate.value)) {
                alert('From Date shouldnt be greater than To Date');
                return false;
            }
            else
                return true;
        }
        else {
            alert('Invalid Date');
            return false;
        }
    }
    else {
        if (chk.test(fromDate.value)) {
            if (FormatDate(fromDate.value) > FormatDate(document.getElementById("hdnTodayDate").value)) {
                alert('Selected Date should be less than or equal to current date');
                return false;
            }
            else
                return true;
        }
        else {
            alert('Invalid Date')
            return false;
        }
    }
}
function FormatDate(val) {
    var tmpDate = val.split("/");
    var DD = tmpDate[0];
    var MM = tmpDate[1];
    var YYYY = tmpDate[2];
    var fDate = new Date(MM + "/" + DD + "/" + YYYY);
    return fDate;
}


calling of function
on pageload event

C#
btnSave.Attributes["onClick"]="return CheckValidDate(txtfromDate.Text, toDate.Text);
 
Share this answer
 
v3
Comments
rathijeya1 16-May-12 9:26am    
I want start date end Date validation for Date format (dd-MMM-yyyy)

Thanks in advance..
Rathi.
Nilesh Patil Kolhapur 24-May-12 2:20am    
rathijeya1 mail me ur Problem on ndpatil999@gmail.com
try this js function
JavaScript
function valDate() {
            try {
                var d1 =frm.txtFrmDate.value.substr(0, 2);
                var m1 = frm.txtFrmDate.value.substr(3, 2);
                var y1 = frm.txtFrmDate.value.substr(6, 4);
                var StrDate = m1 + "/" + d1 + "/" + y1;

                var d2 = frm.txtToDate.value.substr(0, 2);
                var m2 = frm.txtToDate.value.substr(3, 2);
                var y2 = frm.txtToDate.value.substr(6, 4);
                var EndDate = m2 + "/" + d2 + "/" + y2;

                var startDate = new Date(StrDate);
                var endDate = new Date(EndDate);
                if (startDate > endDate) {
                    alert('To date should be greater than From date.');
                    frm.txtFrmDate.value = '';
                    frm.txtToDate.value = '';
                    frm.txtFrmDate.focus();
                    return false;
                }
            } catch (e) { alert(e.Description); }
        }
 
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