Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery form validation with .bind('submit')

I am trying to get form validation and .bind('submit') (to replace submit button with spinner) to play nicely together.

My JS code is

    $.metadata.setType("attr", "validate");
    $(document).ready(function() {
        $("#selecttest").validate({
            rules: {
                field: {
                    required: true,
                    email: true
                    }
                }
            });

        $('#selecttest').bind('submit', function() {
            $('#div-loading').show();
            $('#div-submit').hide();
        });

    });

and the form is:

<form id="selecttest" method="post"  action="submit.php">
Please enter your Email:
<input type="email" name="email" id="email" style="font-size: 30px;" class="inputtext required email" size="30" onKeyDown="if(event.keyCode==13) event.keyCode=9;" validate="required:true" title=""><br><br>
                <input type="checkbox" name="tos" value="tos" class="required" checked/>I agree to the <a>terms and conditions</a><br>
                <div id="div-loading" style="display:none;">
                    <img src="../images/loading.gif" class="Loading">
                </div>
                <br>
                <div id="div-submit">
                    <INPUT type="submit" value="Submit" class="ButtonSubmit">
                </div>
            </form> 

And both functions work independently of each other very well, I just can not figure out how to tie the second (show/hide) in that it only runs once validated and submitted.

like image 793
Wulf Solter Avatar asked Dec 05 '25 05:12

Wulf Solter


1 Answers

http://docs.jquery.com/Plugins/Validation/validate

$("#selecttest").validate({
    rules: {
        field: {
            required: true,
            email: true
        }
    },
    submitHandler: function(form) {
        $('#div-loading').show();
        $('#div-submit').hide();
        form.submit();
   }
});
like image 105
Joseph Avatar answered Dec 07 '25 19:12

Joseph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!