0

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();");
1
  • try "onclick". It's the correct way to specify the attribute, even if it may work with the capital C. Commented Apr 7, 2012 at 8:25

1 Answer 1

2

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..

Sign up to request clarification or add additional context in comments.

2 Comments

thank u for the correct answer..Can u explain how is it working ??
I appended the explanation..@ksg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.