Wednesday, August 13, 2014

validation fromdate and todate with future date validation in javascript

 <script type="text/javascript">
        //This code is developed to validation fromdate and todate with future date validation in javascript in a web page.



        function ValidateDate() {
            var Fromdate = document.getElementById('<%= txtFromdate.ClientID %>').value;
       
            var Todate = document.getElementById('<%= txtTodate.ClientID %>').value;
         

            if (Fromdate == "") {
                alert("From date should not be empty");
                return false;
            }
            else if (check_futuredate(Fromdate) == false) {
                alert("From date is greater than today's date ");
                return false;
            }
            if (Todate == "") {
                alert("To date should not be empty");
                return false;
            }
            else if (check_futuredate(Todate) == false) {
                alert("To date is greater than today's date ");
                return false;
            }
            if ((Date.parse(Fromdate) == Date.parse(Todate))) {
               // alert("To date should be greater than From date");
                return true;
            }
            else if ((Date.parse(Todate) <= Date.parse(Fromdate))) {
                alert("To date should be greater than From date");

                return false;
            }
            
        }

        function check_futuredate(chkdate) {
            var chkdate = chkdate; //document.getElementById('<%= txtFromdate.ClientID %>').value;

            var edate = chkdate.split("-");
            var spdate = new Date();
            var sdd = spdate.getDate();
            var smm = spdate.getMonth();
            var syyyy = spdate.getFullYear();
            var today = new Date(syyyy, smm, sdd).getTime();
            var e_date = new Date(edate[2], edate[1] - 1, edate[0]).getTime();
            if (e_date > today) {
                //alert("From date is greater than today's date ");
                return false;
            }
           

        }
    </script>
 <asp:Button ID="btnSubmit" runat="server" Text="Submit"                         Style="width: 61px; color: #FFFFFF;"  BackColor="#FF6804"  OnClientClick="return ValidateDate();"                        OnClick="btnSubmit_Click" />

No comments:

Post a Comment