7

How can you disable the send -button if there is one or more input -fields empty?

My attempt in pseudo-code

if ( $("input:empty") ) {
    $("input:disabled") 
}
else 
  // enable the ask_question -button

I have been reading these articles without finding a right solution

  1. Official docs about empty: this is like not relevant because it looks for all input -fields
  2. a thread about disabled -feature in jQuery: this seems to be relevant
  3. to be able to test jQuery Firefox/terminal: to get a testing environment would help me most

I use at the moment the following code. It contains one bug about the character ;, but I cannot find it.

#1

$(document).ready(function(){
    $("#ask_form").validate(){
        rules: {
            username {
                required: true,
                minlenghth: 2
            },
            email: {
                required: true;
                minlength: 6
            },
            password {
                required: true,                                                                                                                                    
                minlength: 6
            }
        } 
    });
}

#2 Meder's code

I slighhtly modified Meder's code. It disables the send -button permanently so it has a bug too.

$(document).ready(function(){
    var inputs = $('input', '#ask_form'), empty = false;
    // can also use :input but it will grab textarea/select elements and you need to check for those..

    inputs.each(function() {
        if ( $(this).val() == '' ) {
            empty = true;
            return;
        }
    });

    if ( empty ) {
        $('.ask_question').attr('disabled', 'disabled'); // or true I believe.
    }
});

4 Answers 4

10
var $submit = $("input[type=submit]");
if ( $("input:empty").length > 0 ) {
   $submit.attr("disabled","disabled");
} else {
   $submit.removeAttr("disabled");
}
Sign up to request clarification or add additional context in comments.

8 Comments

Just make sure to put this in an onChange event or similar so it will fire each time a field is changed.
Your code does disable the buttons. However, it does disable buttons too when the fields are not empty. - How do you read this input:empty).length > 0 in English? - My attempt to apply the given rule to the tag input which is empty such that its length is greater than zero. - This does not make sense to me, since my consideration leads to a contradiction.
I removed .length > 0, but the buttons remain disabled.
what this does is gets a collection of all the input elements that have no children, including text nodes. the length property just determines how many items are in the collection. if there are 0 items in the collection, the length will be 0. you can obviously be more specific in your selector, like $("input.myInputs:empty") but this should do the trick.
i updated my code so that if everything is ok but previously was not, the button becomes enabled again
|
2

jQuery validation plugin
Documentation
Demo

1 Comment

Thank you for the links! I am using the code in the demo at the moment. There is one bug in my code of the question.
0
var inputs = $('input', 'form#foo'), empty = false;
// can also use :input but it will grab textarea/select elements and you need to check for those..

inputs.each(function() {
    if ( $(this).val() == '' ) {
        empty = true;
        return;
    }
});

if ( empty ) {
    $('#el').attr('disabled', 'disabled'); // or true I believe.
}

1 Comment

Your code has the same problem as Jason's. It disables the send -button permanently.
-1

Missing the closing, try this for example #1:

$(document).ready(function(){
    $("#ask_form").validate(){
        rules: {
            username {
                required: true,
                minlenghth: 2
            },
            email: {
                required: true;
                minlength: 6
            },
            password {
                required: true,                                                                                                                                    
                minlength: 6
            }
        } 
    });
});

Comments

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.