jQuery validation on rows with text input and select fields?

10,065

how about something like this?

you were nearly there, just needed a way to target select fields and find out how they trigger events - .change(). once you have that, you can find the .val() of the selects and compare it to NA (the equivalent of empty/null/not selected) and then add has-error accordingly.

$(document).ready(function() {
    $("#mytable").on("change", "input, select", function(){    
        var $selects = $(this).closest('tr').find('td select'),
            $cells = $(this).closest("tr").find("td input");
        $cells.removeClass("has-error");
        $selects.removeClass("has-error");
        $cells.each(function() {
            if ($(this).val().trim() === '') {
                $(this).addClass("has-error");
            }
        });
        $selects.each(function() {
            console.log($(this).val() == 'NA');
            if ($(this).val() == 'NA') {
                $(this).addClass("has-error");
            }
        });
    });
});
.has-error{
    
border-style: solid;
    border-color: #ff0000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<table id="mytable">
    <tr>
        <th>Row</th>
        <th>ID</th>
        <th>Date</th>
        <th>Yes/No</th>
    </tr>
    <tr>
        <td>1</td>
        <td>
            <input type="TEXT" id="ID1" name="ID1" class="target ids" />
        </td>
        <td>
            <input type="TEXT" id="DATE1" name="DATE1" class="target"  />
        </td>
        <td>
            <select id="YN1" name="YN1" class="target" >
                <option value="NA">NA</option>
                <option value="Yes">Yes</option>
                <option value="No">No</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>2</td>
        <td>
            <input type="TEXT" id="ID2" name="ID2" class="target ids" />
        </td>
        <td>
            <input type="TEXT" id="DATE2" name="DATE2" class="target" />
        </td>
        <td>
            <select id="YN2" name="YN2" class="target" >
                <option value="NA">NA</option>
                <option value="Yes">Yes</option>
                <option value="No">No</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>3</td>
        <td>
            <input type="TEXT" id="ID3" name="ID3" class="target ids" />
        </td>
        <td>
            <input type="TEXT" id="DATE3" name="DATE3" class="target"  />
        </td>
        <td>
            <select id="YN3" name="YN3" class="target" >
                <option value="NA">NA</option>
                <option value="Yes">Yes</option>
                <option value="No">No</option>
            </select>
        </td>
    </tr>
</table>

Share:
10,065
user2072931
Author by

user2072931

Updated on June 04, 2022

Comments

  • user2072931
    user2072931 almost 2 years

    I am a jQuery/JavaScript beginner and trying to create a spreadsheet-style form with validation so that, once information has been entered for any field in the row, all fields in that row should be required too. This post and one of the suggestions posted there were extremely helpful in terms of how to handle validation of text input fields in the row. However, I also need to apply that validation logic to select fields in the row as well. I've tried changing the settings in the .on() and .find() functions, but am having no luck. How could this script (taken from the post above) be changed to handle the select fields in the row as well as the text input fields? Here is a demo with the row validation working for the text inputs, but not the select fields. Thank you!

    $("#mytable").on("input", "input", function(){
        var anySet = false,
            $cells = $(this).closest("tr").find("td input");
        $cells.each(function() {
            var somethingInCell = $(this).val().trim() !== '';
            anySet |= somethingInCell;
        });
        $cells.removeClass("has-error");
        if (anySet) {
            $cells.each(function() {
                if ($(this).val().trim() === '') {
                    $(this).addClass("has-error");
                }
            });
        }
    });