We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,664 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Enable/disable submit button

Hi, I'm trying to enable a submit button only when all the input fields are filled. Is this the correct way of checking to see if an input field is empty?

$(document).ready(function() {
     $('#submit_button').attr('disabled', 'disabled');

     $.each(':input', function() {
        if ($(this).val() != "") {
            $('#submit_button').removeAttr('disabled');
        }
     });
});
3
Contributors
5
Replies
2 Hours
Discussion Span
3 Months Ago
Last Updated
8
Views
Question
Answered
NoUserNameHere
Light Poster
42 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Yes, however if any of the fields are not empty, this will validate and set the submit button to be enabled.

Try something like this instead.

$(document).ready(function() {
     $('#submit_button').attr('disabled', 'disabled');

     var isValid = true;

     $.each(':input', function() {
        if ($(this).val() == "") {
            isValid = false;

            break;
        }
     });

     if(isValid){
        $('#submit_button').removeAttr('disabled');
     }
     else{
        alert('You have empty fields.');
     }
});
stbuchok
Practically a Posting Shark
875 posts since May 2011
Reputation Points: 138
Solved Threads: 124
Skill Endorsements: 2

Hi NoUserNameHere,
Yes, $(this).val() != "" this is a correct way on how to check if the value is empty in jQuery.

It seems you have a problem with your each. Should it be this instead?

$.each($(':input'), function() {
    if ($(this).val() != "") {
        $('#submit_button').removeAttr('disabled');
    }
});

The code you provided,$.each(':input', function() {, will just iterate over the ':input' string.

gon1387
Posting Whiz in Training
233 posts since Jan 2011
Reputation Points: 32
Solved Threads: 37
Skill Endorsements: 3

Tried the mentioned methods that were posted above and the submit button remained enabled for some reason. Hmm, can't figure out why its doing that.

NoUserNameHere
Light Poster
42 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

The issue with the code is, wheneever there's an input which was set not to have any value at all, the #submit_button just won't be enable.
You can add a class for checking what input element should be checked.Here's an example.
Your HTML:

<form>
<input type="text" class="checkThis" name="input1" id="input1" value="" />
<input type="text" name="input2" id="input2" value="" />
<input type="text" class="checkThis" name="input3" id="input3" value="" />
<input type="text" class="checkThis" name="input4" id="input4" value="" />
<input type="text" name="input5" id="input5" value="" />
<input id="submit_button" type="submit" value="sub" />
<form>

Your Javascript:

$(document).ready(function() {
     $('#submit_button').attr('disabled', 'disabled');

     //Attach an on change event handler to all input
     // with a checkThis class
     $('input.checkThis').on('change',function(){
        //employing also stbuchoks technique
        var hasEmptyInput = false;

        //check each input for empty value
        $('input.checkThis').each(function(){

            //breakout from 'each' if there was an empty value
            // cause there not much to do, since we proved there was an
            // empty value
            if ($(this).val() == "") {
                hasEmptyInput = true;
                return false;
            }
        });

        // Great!! all has value
        if(!hasEmptyInput){
            $('#submit_button').removeAttr('disabled');
        // OH, snap! There's an empty input again
        } else {
            $('#submit_button').attr('disabled', 'disabled');
        }
     });
});

Here's the JSFIDDLE EXAMPLE: JS FIDDLE EXAMPLE LINK

gon1387
Posting Whiz in Training
233 posts since Jan 2011
Reputation Points: 32
Solved Threads: 37
Skill Endorsements: 3

I understand now. Thank you!

NoUserNameHere
Light Poster
42 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 3 Months Ago by gon1387 and stbuchok

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.3188 seconds using 2.68MB