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.
The problem is that post-back is occuring even if the validation returns false.....
I'm new to javascript.Kindly give me any solution.....
Here's my code
<script type="text/javascript">
function ValidateDate() {
var GridView = document.getElementById('GridView1');
var i = 1;
var rows = GridView.rows.length;
for (var j = 0; j < 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 < FromDate) {
document.getElementById('GridView1_ctl0' + i + '_txtFrom').focus();
alert('ToDate is less than from date');
return false;
}
}
return true;
}
</script>
I've called javascript on pageload event
this.btnSave.Attributes.Add("onClick", "ValidateDate();");