user351657
user351657

Reputation: 361

Jquery form :input

Is there a "simple" way to ensure that ALL the inputs in a form have "an entry". I need/want to disable the submit button unless all inputs have a value they are all input type=text. I assume it is something like for each but I am not very good at the for each in JQuery. Possible problem is that the inputs are "dynamically" generated by sortables? All I need to know is is there a value in every input field. The "real" validation is done elsewhere.

So is it something like:

$j('#button').click(function (){
each(function(:input){
//check the length in here?
});
});

Upvotes: 2

Views: 177

Answers (2)

Tarka
Tarka

Reputation: 4043

$("#button").click(function(event) {
    var valid = true;
    $(":input").each(function(index) {
        if ($(this).val().length == 0) {
            valid = false;
        }
    });
    if (!valid) {
        event.preventDefault();
    }
});

Should work.

First part grabs the element with id of button, then assigns a function to the onclick event. Second part grabs all input fields, and run a function on each of them. Then you get the length of the value for each.

The $(this) works since the function is being applied to a specific element on the page, and will always get you the current element.

Upvotes: 1

Related Questions