Enabling disabled button after successful jQuery validation

LJT

I have the following code where I have 2 email input fields which I need to validate they are the same which is working successfully using jQuery validate equalTo.

<div class="control-group">
 <label class="control-label" for="inputEmail"><strong>Email Address</strong></label>
   <div class="controls">
    <input type="email" name="inputEmail" placeholder="[email protected]" id="inputEmail" required>
   </div>
    <label class="control-label" for="inputEmailConfirm"><strong>Confirm Email Address</strong></label>
     <div class="controls">
    <input type="email" name="inputEmailConfirm" placeholder="[email protected]" id="inputEmailConfirm" required>
    </div>
    </div>
  <button type="submit" id="emailSubmit" disabled="disabled" class="btn btn-danger" data-toggle="tooltip" data-placement="bottom" title="Click me to buy">Credit Card Checkout &raquo; </button>

Upon successful validation I wish to enable the button, but can't seem to figure out how to do this.

 $('#ccSelectForm').validate({
    rules: {
      inputEmail: {
        required: true,
        email: true
      },
      inputEmailConfirm: {
        required: true,
        email: true,
        equalTo: '#inputEmail'
      },
    }
  });

Ideally as bonus I'd like to set the form controls to utilise the validation states in Bootstrap3, detailed here - http://getbootstrap.com/css/#forms-control-validation

Fiddle is here http://jsfiddle.net/4wU5m/

Sparky

There is no jQuery Validate plugin callback option/function that fires when all fields are valid without first clicking the submit button.

You'll need to create an external keyup blur event handler that checks your form for validity and enables/disables the button accordingly; every time a key is pressed or you leave a field.

DEMO: http://jsfiddle.net/p628Y/1/

$(document).ready(function () {

    $('#ccSelectForm').validate({
        rules: {
            inputEmail: {
                required: true,
                email: true
            },
            inputEmailConfirm: {
                // required: true,  // <-- redundant
                // email: true,     // <-- redundant
                equalTo: '#inputEmail'
            }  // <-- removed trailing comma
        }
    });

    $('#ccSelectForm input').on('keyup blur', function () { // fires on every keyup & blur
        if ($('#ccSelectForm').valid()) {                   // checks form for validity
            $('button.btn').prop('disabled', false);        // enables button
        } else {
            $('button.btn').prop('disabled', 'disabled');   // disables button
        }
    });

});    

Since the jQuery Validate plugin disables the browser's HTML5 validation dynamically, I removed the required attribute from your markup and changed type="email" into type="text". (You already have these validation rules specified in the plugin.)

<input type="text" name="inputEmail" placeholder="[email protected]" id="inputEmail" />
<input type="text" name="inputEmailConfirm" placeholder="[email protected]" id="inputEmailConfirm" />

You also don't need to specify all the rules twice when using the equalTo rule as the second field must already always match the first.


EDIT:

In the above scenario, your page is totally dependent upon JavaScript. In other words, without JavaScript, the button is always disabled.

If you have server-side validation in place and want your page to be independent of JavaScript, you'll need to remove the disabled="disabled" from your button markup and add it to your JavaScript just inside the DOM ready event handler.

$('button.btn').prop('disabled', 'disabled');

In this scenario, without JavaScript, you'll still have a working button, and with JavaScript, the button is disabled programatically on page load.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Enabling disabled button after successful jQuery validation

From Dev

JQuery enabling button if all checkboxes are selected and add css to disabled button

From Dev

Re-enable jQuery validation after disabled

From Dev

Enabling a disabled button by clicking a different button

From Dev

Form Validation and disabled button

From Dev

ng-disabled nor enabling Submit button

From Dev

Keep submit button disabled while jquery.validation errors are true

From Dev

Remove button on submit (after validation) using jquery

From Dev

Trouble updating a <div> after successful validation using jQuery Validation plug-in

From Dev

jquery prop doesn't disabled after clicked button

From Dev

Complex validation and submit button enabling in AngularJS

From Dev

Disabled button with jquery mobile

From Dev

disabled/enabled button - jquery

From Dev

Disabled and enabled button in jquery

From Dev

Strong Name validation fails after enabling LARGEADDRESSAWARE

From Dev

re enabling ng-disabled button in Angular JS

From Dev

Enabling custom button (disabled by default) when row is selected

From Dev

Enabling an HTML button after a certain condition

From Dev

Resetting aurelia-validation after a successful submit

From Dev

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

From Dev

JQuery enabling a textbox with a button event query

From Dev

Jquery UI tooltip on disabled button

From Dev

Codeigniter : bootstrap disabled changes to enabled after validation

From Dev

Enabling/Disabling Button based on the validation of the text box is not working

From Dev

Change button name after successful insertion

From Dev

Not able to enable a button after it is disabled in java swing

From Dev

Android: after installing an APK, the open button is disabled

From Dev

Android - Having button disabled after second usage

From Dev

JS can't enable the button after disabled

Related Related

  1. 1

    Enabling disabled button after successful jQuery validation

  2. 2

    JQuery enabling button if all checkboxes are selected and add css to disabled button

  3. 3

    Re-enable jQuery validation after disabled

  4. 4

    Enabling a disabled button by clicking a different button

  5. 5

    Form Validation and disabled button

  6. 6

    ng-disabled nor enabling Submit button

  7. 7

    Keep submit button disabled while jquery.validation errors are true

  8. 8

    Remove button on submit (after validation) using jquery

  9. 9

    Trouble updating a <div> after successful validation using jQuery Validation plug-in

  10. 10

    jquery prop doesn't disabled after clicked button

  11. 11

    Complex validation and submit button enabling in AngularJS

  12. 12

    Disabled button with jquery mobile

  13. 13

    disabled/enabled button - jquery

  14. 14

    Disabled and enabled button in jquery

  15. 15

    Strong Name validation fails after enabling LARGEADDRESSAWARE

  16. 16

    re enabling ng-disabled button in Angular JS

  17. 17

    Enabling custom button (disabled by default) when row is selected

  18. 18

    Enabling an HTML button after a certain condition

  19. 19

    Resetting aurelia-validation after a successful submit

  20. 20

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

  21. 21

    JQuery enabling a textbox with a button event query

  22. 22

    Jquery UI tooltip on disabled button

  23. 23

    Codeigniter : bootstrap disabled changes to enabled after validation

  24. 24

    Enabling/Disabling Button based on the validation of the text box is not working

  25. 25

    Change button name after successful insertion

  26. 26

    Not able to enable a button after it is disabled in java swing

  27. 27

    Android: after installing an APK, the open button is disabled

  28. 28

    Android - Having button disabled after second usage

  29. 29

    JS can't enable the button after disabled

HotTag

Archive