Question

I have used jQuery.validator.addMethod to create a custom validate method that will make sure there is at least one row in my data-entry table. Using Firebug, my breakpoint in the method isn't being hit at all. Am I missing something?

Note: I am sure there are problems with the validation method itself, but that is because I can't debug it yet. I am still very much a novice with JavaScript.

Update: I have figured out that it isn't getting triggered because I didn't have the validator on an input field. I have it working now, however, is there a way to add a validation without attaching it to an input field?

Here is what my table looks like:

<table id="editorRows">
    ...
    <tbody class="editorRow">
        <tr class="row1">
        </tr>
        <tr class="row2" style="display: none;">
        </tr>
        <tr class="row3" style="display:none;">
        </tr>
    </tbody>
    <tbody class="editorRow">
        <tr class="row1">
        </tr>
        <tr class="row2" style="display: none;">
        </tr>
        <tr class="row3" style="display:none;">
        </tr>
    </tbody>
</table>

Here is my custom validator:

jQuery.validator.addMethod('required_row', function(val, el) {
    var rowCount = $("#editorRows tbody").length;
    if (typeof rowCount != "undefined") {
        return (rowCount > 0);
    } else {
        return false;
    };
}, "Your request must contain at least one day.");

// Validate the form before submitting
$("form").validate({
    rules: {
        "#editorRow": { required_row: true }
    }
});
Was it helpful?

Solution

I have figured out that it isn't getting triggered because I didn't have the validator on an input field. I have it working now, however, is there a way to add a validation without attaching it to an input field?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top