0

On this code, it does not allow to save if any of table cell is empty, however, I want it now to save even the last table cell is null.

How can i add here for last input of table row where type is not hidden?

 $('#myTable tr input[value != add]:text').filter(function () {});

see my FIDDLE

This is my javascript code :

 $("#btnSave").click(function (event) {
        var flag = false;
        var emptyBoxes;
        var $rows = $('#myTable tr:not(:hidden)');
        $rows.each( function () {
            emptyBoxes = $('#myTable tr input[value != add]:text').filter(function () {
                return this.value == "";    
            });

            if (emptyBoxes.length != 0) {
                flag = true;
            }
        });
        if (flag) {
            alert("this cannot be empty");
            emptyBoxes.eq(0).focus();
        } else 
            alert("done");
    });
bumbumpaw
  • 2,522
  • 1
  • 24
  • 54

1 Answers1

2

Try the following code:

var fields = $('input[type=text]'); /* All fields */

/* This function will find empty fields */
var findEmptyFields = function() {

    var n = fields.length - 1;

    /* Go throw all inputs with type=text and if someone is empty return index of this element */
    for (var i = 0; i < n; i++) {
        if (fields.eq(i).val() === '') {
            return i;
        }
    };

    /* Else return false that means that we didn't find any empty fields*/
    return false;


}

$("#btnSave").on('click', function() {

    var empty = findEmptyFields();
    if (empty === false) {
        alert('Done');
    }
    else {
        alert('Some field is empty');
        fields.eq(empty).focus();
    }

});