javascript - 表单输入的条件验证

标签 javascript jquery validation formvalidation-plugin

首先有一个追加按钮,该按钮生成一行,其中包含 1 个选择框、1 个输入框和 4 个复选框。添加此行的限制最多为 1-10 行。我不知道如何使用例如 http://formvalidation.io/ 进行 jquery 验证- 或独立的 jquery 代码。我想应用的规则:

如果选择的 Angular 色是用户(不是管理员),我必须验证至少选中了一个复选框,并且用户不会在选择中出现两次

问题是我什至不知道从哪里开始,你能给我一些提示吗?

实例:: http://jsfiddle.net/Yy2gB/131/

附加方法 onClick

$(document).ready(function(){
    var obj = {"1":"Admin istrator","2":"User2"};

    //$('.selectpicker').selectpicker();
    $(".addCF").click(function(){

        count = $('#customFields tr').length + 1;
        var sel = $('<select name="user'+count+'">');

        for(key in obj){
            // The key is key
            // The value is obj[key]
            sel.append($("<option>").attr('value',key).text(obj[key]));
        }

        $('.selectpicker').selectpicker();
        $("#customFields").append('<tr><td>'+sel[0].outerHTML
        +'</td><td><input class="form-control" class="valid_role"'
        +' data-fv-field="emails" type="text" name="role'+count
        +'" /></td><td><input type="checkbox" class="mycheckbox"'
        +' name="can_edit'+count+'"></td><td><input type="checkbox" '
        +'class="mycheckbox" name="can_read'+count+'"></td><td><input '
        +'type="checkbox" class="mycheckbox" name="can_execute'+count+'">'
        +'</td><td><input type="checkbox" class="mycheckbox" '
        +'name="is_admin'+count+'"></td><td><a href="javascript:void(0);"'
        +'class="remCF">Remove</a></td></tr>');
        $('.mycheckbox').iCheck({
            checkboxClass: 'icheckbox_square-blue',
            radioClass: 'iradio_square-blue'
        });
        $('.selectpicker').selectpicker();
    });
    $("#customFields").on('click','.remCF',function(){
    $(this).parent().parent().remove();
    });
});

HTML 表单

<div class="col-md-12 col-lg-12"> 
  <table class="table table-user-information" id="customFields">
    <thead>
      <tr>
        <th class="standardTable_Header">User</th>
        <th class="standardTable_Header">Role</th>
        <th class="standardTable_Header">
          <span title="administrator projektu">Can read</span>
        </th>
        <th class="standardTable_Header">
           <span title="uprawnienie do edycji danych projektu">
             edit
           </span>
        </th>
        <th class="standardTable_Header">
          <span title="uprawnienie do odczytu danych projektu oraz przypisanych do niego zadań">
           excute
          </span>
        </th>
        <th class="standardTable_Header">
          <span title="uprawnienie do edycji danych projektu">
            admin
          </span>
         </th>
      </tr>
    </thead>
  <tbody>
    <button type="button" class="btn btn-default addCF">
      Append
    </button>
    </div>
  </tbody>
</table>

最佳答案

使用这个jQuery Validation Plugin并通过this demo ,我们可以执行以下操作:
假设:当且仅当 select 标记的值为 User2 时, Angular 色复选框必须至少选中一个 1- 使用带有提交按钮的 form 标签包装您的表格:

<div class="col-md-12 col-lg-12">
    <form id="myform">
        <table class="table table-user-information" id="customFields">
            ...
        </table>
        <input type="submit" value="submit" />
    </form>
</div>

2- 我们需要编辑复选框 html,使它们具有相同的名称但具有不同的值,例如:

<input type="checkbox" class="mycheckbox" name="ourRoles" value="can_edit' ...>
<input type="checkbox" class="mycheckbox" name="ourRoles" value="can_read' ...>

等等。

3-添加以下脚本:

// just for the demos, avoids form submit
    jQuery.validator.setDefaults({
        debug: true,
        success: "valid"
    });
    $("#myform").validate({
        rules: {
            ourRoles: {
                required: function () {
                    if ($("select[name=user2]").val() == 2) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        }
    }); 

通过 this fiddle 一起查看所有内容

关于javascript - 表单输入的条件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31962403/

相关文章:

javascript - 如何定义一次性事件,以便即使在事件已经发生后新的处理程序也会触发(一次)?

php - 在同一个项目中结合 Eclipse PDT 和 Aptanta 插件 w/JQuery

C 编程 : How to check if user has entered numeric values only in the following expression?

javascript - 如何向输入字段添加验证?绑定(bind) JSON 模型不起作用

javascript - 传递、访问或预填充数据时遇到问题 - Vue.js/Firestore

javascript - Grunt 'grunt-bower-concat',忽略 mainFiles 选项

Javascript:在对象数组中的对象上调用方法

javascript - 如何将循环中的本地数组连接到循环外的本地数组?

JavaScript 数组和随机数

jQuery:检查用户名是否存在无效字符