我没有成功创建自定义jQuery验证规则

时间:2022-11-30 15:36:55

I'd like to create my own validation rule for a jqGrid table, but my code does not work. Why?

我想为jqGrid表创建自己的验证规则,但我的代码不起作用。为什么?

This is the HTML:

这是HTML:

<form id="frmTCstepCategorie">
    <fieldset class="ui-widget ui-widget-content">
        <legend class="ui-widget">
            Categorie
        </legend>
        <table id="tableCategories" name="tableCategories">
            <tr>
                <td></td>
            </tr>
        </table>
        <div style="margin-top: 10px" class="categoriesNav">
            <input type="button" id="btnAddCategory" value="Aggiungi"/>
            <input type="button" id="btnDelCategory" value="Rimuovi"/>
        </div>
    </fieldset>
</form>

This is the JavaScript code:

这是JavaScript代码:

$.validator.addMethod("categories", function(value, element) {
    console.log("Checking...");
    return true;
}, "* Required.");

$("#frmTCstepCategorie").validate({
    rules : {
        "tableCategories" : {
            categories : true
        }
    }
});

if ($("#frmTCstepCategorie").valid()) {
    console.log("Checked!");
}

UPDATE

@Sparky told me the right thing:

@Sparky告诉我正确的事:

Using the jQuery Validate plugin, you can only validate <input>, <select>, <textarea> elements.

使用jQuery Validate插件,您只能验证,

Programmatically, you could copy the data into a hidden input and validate that element instead. You would need to set the ignore option to [] in order to validate hidden elements, and maybe the errorPlacement option to put the messages where you need them.

以编程方式,您可以将数据复制到隐藏的输入中,然后验证该元素。您需要将ignore选项设置为[]以验证隐藏的元素,并且可能需要使用errorPlacement选项将消息放入您需要的位置。

I tried to do that, but with no success.

我试着这样做,但没有成功。

If I add an hidden <input>:

如果我添加隐藏的:

<input type="hidden" id="hiddenCategories" name="hiddenCategories" />

and I modify the ignore parameter:

我修改了ignore参数:

$("#frmTCstepCategorie").validate({
    rules : {
        "hiddenCategories" : {
            categories : true,
            ignore: []
        }
    }
});

It does not work.

这是行不通的。

If I change hidden to text:

如果我将隐藏更改为文本:

<input type="text" id="hiddenCategories" name="hiddenCategories" />

It works

Where is the mistake?

哪里出错了?

1 个解决方案

#1


2  

I'd like to create my own validation rule for a jqGrid table, but my code does not work. Why?

我想为jqGrid表创建自己的验证规则,但我的代码不起作用。为什么?

In your code, you are trying to assign the validation rule to a <table> element.

在您的代码中,您尝试将验证规则分配给

元素。

Using the jQuery Validate plugin, you can only validate <input>, <select>, <textarea> elements. No matter what rules you use, standard or custom, these are the only elements that will work and there is no workaround that would allow you to validate <table> or <td> elements.

使用jQuery Validate插件,您只能验证,

元素的解决方法。

Programmatically, you could copy the data into a hidden input and validate that element instead. You would need to set the ignore option to [] in order to validate hidden elements, and maybe the errorPlacement option to put the messages where you need them.

以编程方式,您可以将数据复制到隐藏的输入中,然后验证该元素。您需要将ignore选项设置为[]以验证隐藏的元素,并且可能需要使用errorPlacement选项将消息放入您需要的位置。


EDIT:

$("#frmTCstepCategorie").validate({
    ignore: [],   // <- ignore is an OPTION, not a rule
    rules : {
        "hiddenCategories" : {
            categories : true
        }
    }
});

#1


2  

I'd like to create my own validation rule for a jqGrid table, but my code does not work. Why?

我想为jqGrid表创建自己的验证规则,但我的代码不起作用。为什么?

In your code, you are trying to assign the validation rule to a <table> element.

在您的代码中,您尝试将验证规则分配给

元素。

Using the jQuery Validate plugin, you can only validate <input>, <select>, <textarea> elements. No matter what rules you use, standard or custom, these are the only elements that will work and there is no workaround that would allow you to validate <table> or <td> elements.

使用jQuery Validate插件,您只能验证,

元素的解决方法。

Programmatically, you could copy the data into a hidden input and validate that element instead. You would need to set the ignore option to [] in order to validate hidden elements, and maybe the errorPlacement option to put the messages where you need them.

以编程方式,您可以将数据复制到隐藏的输入中,然后验证该元素。您需要将ignore选项设置为[]以验证隐藏的元素,并且可能需要使用errorPlacement选项将消息放入您需要的位置。


EDIT:

$("#frmTCstepCategorie").validate({
    ignore: [],   // <- ignore is an OPTION, not a rule
    rules : {
        "hiddenCategories" : {
            categories : true
        }
    }
});