Click here to Skip to main content
15,887,998 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello Friends,
i have written a javascript code to compare 2dates from 2 textboxes the problem is that the page is running well in chrome but when i run my application in IE it throw an error "0x800a138f - JavaScript runtime error: Unable to get property 'value' of undefined or null reference"
please help me to overcome from this problem.
Thanks In Advance.
my java script code is
JavaScript
function CompareDates() {
        var fdate = document.getElementById('txtFromDate');
        var edate = document.getElementById('txtToDate');
        var FromDate = fdate.value.split('/');
        var EndDate = edate.value.split('/');
        var val = 'false';

        if (parseInt(FromDate[2]) < parseInt(EndDate[2])) {
            val = 'true';
            return true;
        }
        else if (parseInt(FromDate[2]) == parseInt(EndDate[2])) {
            if (parseInt(FromDate[0]) < parseInt(EndDate[0])) {
                val = 'true';
                return true;
            }
            else if (parseInt(FromDate[0]) == parseInt(EndDate[0])) {
                if (parseInt(FromDate[1]) <= parseInt(EndDate[1])) {
                    val = 'true';
                    return true;
                }
            }
        }
        if (val == "false") {
            alert("FromDate Always Less Than ToDate");
            return false;
        }
    }

and html code is
ASP.NET
<asp:TextBox ID="txtFromDate" runat="server" Width="150px" />
                    <ajaxToolkit:CalendarExtender CssClass="MyCalendar"  runat="server" ID="ceFromDate" TargetControlID="txtFromDate" Format="dd/MM/yyyy" />
<asp:TextBox ID="txtToDate" runat="server" Width="150px" />
                    <ajaxToolkit:CalendarExtender CssClass="MyCalendar"  runat="server" ID="ceToDAte" TargetControlID="txtToDate" Format="dd/MM/yyyy" />

 <asp:Button ID="btnGenerate" runat="server" CssClass="button" Text="Generate" OnClientClick="if(!CompareDates()) return false;" OnClick="btnGenerate_Click" />
Posted

1 solution

sorry..!!
The error is here:
JavaScript
var fdate = document.getElementById('txtFromDate'); 
var edate = document.getElementById('txtToDate');


The problem is that txtFromDate and txtToDate are the server name of controls, not the client name.

i have Tried this:
JavaScript
var fdate = document.getElementById('<%=txtFromDate.ClientID%>');
var edate = document.getElementById('<%=txtToDate.ClientID%>');


Now It is Working Well.
:)
Happy Coding.
 
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