Allow and Prevent postback in javascript in asp.net
<p>I was trying to validate dates using java script on button click event.If validation returns false post-back should be prevented and if returns true post-back should be allowed.</p>

<p>The problem is that post-back is occuring even if the validation returns false.....</p>

<p>I'm new to javascript.Kindly give me any solution.....</p>

<p>Here's my code</p>

<pre><code>&lt;script type="text/javascript"&gt;
    function ValidateDate() {

        var GridView = document.getElementById('GridView1');           
        var i = 1;
        var rows = GridView.rows.length;           
        for (var j = 0; j &lt; rows - 2; j++) {
            i = i + 1;              
            var FromDate = new Date(document.getElementById('GridView1_ctl0' + i + '_txtFrom').value);
            var ToDate = new Date(document.getElementById('GridView1_ctl0' + i + '_txtTo').value);
            if (ToDate &lt; FromDate) {
                document.getElementById('GridView1_ctl0' + i + '_txtFrom').focus();
                alert('ToDate is less than from date');
                return false;
            }
        }
        return true;      
    }
&lt;/script&gt;
</code></pre>

<p>I've called javascript on pageload event</p>

<pre><code>this.btnSave.Attributes.Add("onClick", "ValidateDate();");
</code></pre>
