Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable/enable submit button after jQuery Validation?

I want to disable the submit button of the form after the "on_click" event, this jscript does it succesfully, but when a incorrect email is validated with jquery and then pressed the submit button, after I make the correction of the email, the button still is disabled. How can i solve this!?

Jscript:

<script src="js/libs/modernizr-2.6.2.min.js"></script>
<script src="lib/jquery-1.11.1.js"></script>    
<script src="lib/jquery.js"></script>
<script src="dist/jquery.validate.js"></script> 
<script>
$.validator.setDefaults({
    submitHandler: function() {
        signupForm.submit();
    }
});

$().ready(function() {
    //Desactiva el submit
    $(".submit").click(function () {
       $(".submit").attr("disabled", true);
       $('#signupForm').submit();
     });

    // validate signup form on keyup and submit
    $("#signupForm").validate({
        wrapper: "div",
        rules: {
            email: {
                required: true,
                email: true
            },
            password: {
                required: true,
                minlength: 5
            }
        },
        messages: {
            email: "Use una cuenta de correo v&aacute;lida",
            password: {
                required: "Ingrese su contraseña",
                minlength: "La contrase&ntilde;a al menos debe tener 5 caracteres"
            }
        }
    });


});
</script>

Form

    <form class="cmxform" id="signupForm" method="get" action="an-olvido-contrasena.asp">
    <input type="hidden" value="1" name="inicializar">
                    <p>
                    <div >
                    Correo electr&oacute;nico<br>
                    <input id="email" name="email" type="email" required placeholder="Tu correo electr&oacute;nico" <%if creado<>"1" then%>autofocus<%else%> value="<%=vEmail%>" <%end if%>><br>
                    </div>
                    <br>
                    <input class="submit" type="submit" value="Aceptar"><br>
                    </p>
    </form>
like image 664
Artemination Avatar asked Feb 27 '15 23:02

Artemination


People also ask

How enable and disable the submit button in jQuery?

click(function () { if ($('#submit-button').is(':disabled')) { $('#submit-button'). removeAttr('disabled'); } else { $('#submit-button'). attr('disabled', 'disabled'); } }); In the above code, enabled is the id of the checkbox used and submit-button is the class name of the submit button used .

How do I make my submit button disabled?

1.1 To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); 1.2 To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.

How do I disable a button in jQuery?

To disable a button with jQuery you need to set the disabled property on the button using the prop method. For example $('. my-button'). prop('disabled', true) .


2 Answers

You cannot disable the button on the click event; because if the form is still invalid when you click the button, you will not be able to click it again. You can only disable it after the form has successfully passed validation.

Use the plugin's submitHandler option for this as it's fired on a button click only when the form has passed validation.

<script>
$(document).ready(function() {

    $("#signupForm").validate({
        wrapper: "div",
        rules: {
            email: {
                required: true,
                email: true
            },
            password: {
                required: true,
                minlength: 5
            }
        },
        messages: {
            email: "Use una cuenta de correo v&aacute;lida",
            password: {
                required: "Ingrese su contraseña",
                minlength: "La contrase&ntilde;a al menos debe tener 5 caracteres"
            }
        },
        submitHandler: function(form) { // <- pass 'form' argument in
            $(".submit").attr("disabled", true);
            form.submit(); // <- use 'form' argument here.
        }
    });

});
</script>

NOTES:

  1. $().ready(function() {... is not recommended as per jQuery documentation. Use $(document).ready(function() {... or $(function() {... instead.

  2. You do not need the required inline HTML attribute when you've already declared the required rule within .validate().

  3. Your submitHandler within setDefaults() was broken. The signupForm.submit() line will do nothing because signupForm is an undefined variable. Define submitHandler within .validate() and use the form argument as provided by the developer.

DEMO: http://jsfiddle.net/6foLxzmc/13/

like image 174
Sparky Avatar answered Nov 09 '22 05:11

Sparky


Bind to the submit event instead of to the button click.

$('#signupForm').submit(function(){
   $(this).find('.submit').prop('disabled',true);
});

Alternatively do the disabling in submitHandler option of plugin

like image 36
charlietfl Avatar answered Nov 09 '22 03:11

charlietfl