简体   繁体   English

如何使用jquery validate进行最少输入的验证?

[英]How can I make validate with minimal one input with jquery validate?

My html code like this : 我的html代码是这样的:

<form method="post" id="form-product">
    <input type="text" id="test-1" name="test[1]" /><br>
    <input type="text" id="test-2" name="test[2]" /><br>
    <input type="text" id="test-3" name="test[3]" /><br>
    <input type="text" id="test-4" name="test[4]" /><br>
    <input type="text" id="test-5" name="test[5]" /><br>
    <button type="submit">Submit</button>
</form>

My javascript code like this : 我的JavaScript代码是这样的:

$(document).ready(function () {
    $('#form-product').validate();
    $('[name^="test"]').each(function () {
        console.log('test')
        $(this).rules('add', {
            required: true,
            messages: {
                required: 'Minimal 1 input'
            }
        });
    });
});

Demo and full code like this : http://jsfiddle.net/oscar11/XTtTP/6/ 演示和完整代码如下: http : //jsfiddle.net/oscar11/XTtTP/6/

I want to make the validation dynamic 我想使验证动态

If the user does not enter anything then the user submit, there will be a message "Minimal 1 input" 如果用户未输入任何内容,则用户提交,将显示一条消息“最少1次输入”

If user inputs in one textbox and submit then it will succeed. 如果用户在一个文本框中输入并提交,则它将成功。

The code I created is still not perfect 我创建的代码仍然不完美

If user input in one textbox then submit, it still have message "Minimal 1 input". 如果用户在一个文本框中输入内容然后提交,则仍然显示消息“最少1个输入”。 There should be no message 应该没有讯息

How I improve my code? 如何改善我的代码?

Your code makes them all required. 您的代码使它们全部必需。 If you only want one field required out of the group, then you'll need to use the require_from_group rule instead. 如果只希望组中的一个字段为必填字段,则需要使用require_from_group规则。

$(document).ready(function () {
    $('#form-product').validate();
    $('[name^="test"]').each(function () {
        console.log('test')
        $(this).rules('add', {
            require_from_group: [1, $('[name^="test"]')],
            messages: {
                require_from_group: 'Minimal 1 input'
            }
        });
    });
});

The require_from_group rule is part of the additional-methods.js file. require_from_group规则是require_from_group additional-methods.js文件的一部分。

DEMO: jsfiddle.net/nw53mw17/ 演示: jsfiddle.net/nw53mw17/

I also suggest that you use the latest version(s) of the plugin. 我还建议您使用该插件的最新版本。 Your jsFiddle is using the really old 1.10, but the latest version is 1.17; 您的jsFiddle使用的是非常老的1.10,但最新版本为1.17; this is a substantial difference. 这是一个很大的差异。


EDIT : 编辑

In order to group the messages together, use the groups option. 为了将消息分组在一起,请使用groups选项。 Since you don't know all the names before runtime, use an .each() to construct this parameter. 由于您在运行时之前并不知道所有名称,因此请使用.each()构造此参数。

// dynamically construct groups parameter
var grp = "";
$('[name^="test"]').each(function () {
    grp += $(this).attr('name') + " ";
});
grp = $.trim(grp);
var myGroups = {TESTS: grp};

// intialize plugin with options
$('#form-product').validate({
    groups: myGroups
});

DEMO 2: jsfiddle.net/nw53mw17/2/ 演示2: jsfiddle.net/nw53mw17/2/

If you do not like the placement of this message on the form, use the errorPlacement option . 如果您不喜欢此消息在表单上的放置,请使用errorPlacement选项

Please read the online documentation for this plugin. 请阅读此插件的在线文档

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

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