Sunday, September 11, 2011

Jquery Validate Form Submit Before Opening Popup Window

Sometimes you want to open a form submit in a new window.  Think facebook extended permissions popup window.  To achieve this you would do something like the following.

<script type="text/javascript">
    $(document).ready(function () {
               if ($('#myform').valid()) {
                window.open('', 'formpopup', 'width=800,height=600,resizeable,scrollbars');
                this.target = 'formpopup';
             });
    });
</script>

However you wouldn’t want to open the popup window unless the form being submitted in valid.

If you are using jquery.validate then its pretty easy to update the above javascript to this:

<script type="text/javascript">
    $(document).ready(function () {
        $('#myform').submit(function () {
            if ($('#myform').valid()) {
                window.open('', 'formpopup', 'width=800,height=600,resizeable,scrollbars');
                this.target = 'formpopup';
            }
        });
    });
</script>

 

Hope this helps someone.

No comments: