简体   繁体   English

验证表单中的jQuery-UI Spinner

[英]Validate a jQuery-UI Spinner within a form

I'm using the "jQuery Validation Plugin" to validate the form fields. 我正在使用“ jQuery验证插件”来验证表单字段。 It works very well. 效果很好。

I added a new field into the form. 我在表单中添加了一个新字段。 This field use a jQuery-UI Spinner, but I have no success to validate it. 该字段使用jQuery-UI Spinner,但无法成功进行验证。

This is th HTML code of the spinner element: 这是spinner元素的HTML代码:

<form id="formAddCategory">
    <input id="spinTeamNumber" name="value" style="display: inline-block; margin: 10px; width: 3em;" size="3" maxlength="3">
</form>

This is the spinner code: 这是微调器代码:

$("#spinTeamNumber").spinner({
    step : 1,
    min : 1,
    numberFormat : "n",
    alignment : 'vertical'
});

This is the validation code: 这是验证代码:

$("#formAddCategory").validate({
    rules : {
        "spinTeamNumber" : {
            required : true,
        }
    },
    messages : {
        "spinTeamNumber" : {
            required : "* Required"
        }
    }
});

[...] [...]

if ($("#formAddCategory").valid()) {
    ...
}

But it does not work. 但这行不通。 I also tried to define a custom validation method, but without success. 我也尝试定义自定义验证方法,但没有成功。

You can only use the name , not the id , of the input element within the .validate() method. 您只能在.validate()方法中使用输入元素的name而不是 id

Your name is defined as value ... 您的name被定义为value ...

<input id="spinTeamNumber" name="value" ....

So you must use value within the .validate() method... 因此,您必须在.validate()方法内使用value ...

$("#formAddCategory").validate({
    rules : {
        value : {  // <-- the input NAME
            required : true,
        }
    },
    messages : {
        value : {  // <-- the input NAME
            required : "* Required"
        }
    }
});

To create the spinner, jQuery UI dynamically adds a container around the original input element, so you'll probably want to use the errorPlacement option to get the message to display on the outside. 要创建微调框,jQuery UI会在原始输入元素周围动态添加一个容器,因此您可能需要使用errorPlacement选项使消息显示在外部。

DEMO: http://jsfiddle.net/6u18p6ju/ 演示: http : //jsfiddle.net/6u18p6ju/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM