开发者

jQuery validate custom method not getting called

开发者 https://www.devze.com 2023-02-20 18:03 出处:网络
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

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">
        开发者_如何学C<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 }
    }
});


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号