Reputation: 4067
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();");
Upvotes: 0
Views: 1530
Reputation: 17194
You forgot to specify return
You should try with:
this.btnSave.Attributes.Add("onClick", "return ValidateDate();");
You are returning the value back from the ValidateDate() so for such a condition you need to specify this at the time of function call, and when it returns false then it stops the execution further..
Upvotes: 2