0

I used this code (from SO) to disable asp.net Web Form button to disable button when clicked

$(document).ready(function () {
    $('[id$=btnSave]').click(function () {
        var button = this;

        setTimeout(function () {
            $(button).attr('disabled', 'disabled');
        }, 100);
    });
});

but when I add jQuery validation & JQuery Validation Groups for WebForms plugins to page,

$(document).ready(function () {
    $("#form1").validateWebForm({
        rules: {
            myDate: {
                customDate: true
            },
            nic: {
                nicNo: true
            }
        }
    });
...
<asp:TextBox ID="txtName" runat="server" CssClass="required" ></asp:TextBox>

It disables the button even when the inputs are not valid. How to avoid disabling the button if the inputs are not valid?

7
  • i think wrapping the click function in the ready is the problem.. Commented Mar 21, 2013 at 4:31
  • jQuery Validate plugin does not use validateWebForm(). The initialization function is just called .validate(). Commented Mar 21, 2013 at 4:34
  • @sasi No it does not even disable the button when I remove the ready function... Commented Mar 21, 2013 at 4:41
  • @Sparky Yes I'm using another plugin JQuery Validation Groups for WebForms. Updated the question. Thanks... Commented Mar 21, 2013 at 4:43
  • You want to disable the button when the inputs are valid? Commented Mar 21, 2013 at 4:47

1 Answer 1

1

Try using the plugin's built-in .valid() method to test the form before disabling the button and submitting. As per comments, I realize you're initializing the jQuery Validate plugin with another plugin, but the core concept below should work the same.

DEMO: http://jsfiddle.net/7HzZF/

jQuery:

$(document).ready(function() {

    $('#myform').validate({
        // rules & options
    });

    $('#button').on('click', function (e) {
        e.preventDefault();
        if ($('#myform').valid()) {
            $('#myform').submit();
            $(this).attr('disabled', 'disabled');
        }
    });

});

HTML:

<form id="myform">
    ....
    <input id="button" type="submit" />
</form>

Alternatively, if your other plugin allows you to use the submitHandler callback function, I strongly recommend using that instead.

http://jsfiddle.net/h34bJ/

It only fires on a valid form and it really clears up a lot of this redundant and superfluous event handling.

$(document).ready(function () {

    $('#myform').validateWebForm({
        // your rules,
        submitHandler: function (form) {
            $('#button').attr('disabled', 'disabled');
        }
    });

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

6 Comments

This disables the button correctly but does not submit the form. May be it needs to accompany setTimeout function used in my original code (but reloads the same page). I tried to add it but still could't make it work.
Its working... Had to add setTimeout function and remove line $('#myform').submit();. However Thanks a lot... :)
@Nalaka526, I'm not sure, although it might be because I forgot to include the preventDefault which removes the submit from the normal click, then only puts it back when the form is valid.
Updated to new code but Uncaught ReferenceError: e is not defined in line e.preventDefault();...
@Nalaka526, e has to be passed into the function where it's called... function(e)
|

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.