简体   繁体   English

如何使用jQuery Validation插件基于输入type = text / radio / checkbox验证表单

[英]How to validate form based on input type=text/radio/checkbox using jQuery Validation Plugin

I have a multi step form with dynamically generated fields that I need to validate using jQuery, this is the form . 我有一个包含动态生成字段的多步骤表单,需要使用jQuery进行验证,这就是表单 How I can achieve this ? 我该如何实现?

// next step
    $('.form-horizontal .btn-next').on('click', function() {
        $("#multiphase").validate();

        $('input[type="text"]').each(function(){
            $(this).rule('add', {
                required: true,
                messages:{
                    required: "This field is required"
                }
            })
        })

    });

First of all, you can validate elements using "name" attribute. 首先,您可以使用"name"属性来验证元素。 you can show below example: 您可以显示以下示例:

<script type="text/javascript">
  $("#pseudoForm").validate({
    onfocusout:true,
    rules:{
      first_name:"required",
      last_name:"required"
    }
  });
</script>
<!-- whatever -->
<div id="pseudoForm">
  <input type="text" name="first_name"/>
  <input type="text" name="last_name"/>
</div>

also you can see reference as this link LINK 您也可以看到此链接的参考链接LINK

You should map the input fields to an object containing the rules for the validation plugin. 您应该将输入字段映射到包含验证插件规则的对象。 I assume you're using this: http://jqueryvalidation.org/ 我假设您正在使用此: http : //jqueryvalidation.org/

Here's the general idea: 这是一般的想法:

  var fields = {};
  $('form').find(':input').each(function() {
    fields[this.name] = "required";
  });

  $('form').validate({
    rules: fields
  });

Of course you have to configure the plugin a bit more, but this is how you will handle the dynamic fields at least. 当然,您必须多配置一些插件,但这至少是您处理动态字段的方式。 An alternative would be to generate the rules server-side. 一种替代方法是在服务器端生成规则。

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

相关问题 根据输入类型=收音机/复选框/选择使用jQuery进行验证 - Validate using jQuery based on input type = radio/checkbox/select 基于输入类型的 jQuery 验证 = radio/checkbox/select - jQuery Validation based on input type = radio/checkbox/select 如何使用jquery验证插件验证名称中带点的输入字段? - How to validate input fields with a dot in name using the jquery validation plugin? jQuery验证,如果复选框选中的文本框获得class =“ form-input validate required” - Jquery validation if checkbox checked text box gets class=“form-input validate required” 如何客户端实时验证表单与选择,广播,复选框,文本,文件输入,只需使用JavaScript? - How to client side realtime validate a form with select, radio, checkbox, text, file input, just use javascript? jQuery Validation Plugin-基于单选按钮的最小文本输入量 - jQuery Validation Plugin - Minimum amount for text input based on radio button checked 如何使用jQuery验证引擎来验证“输入类型文件”? - How to validate “input type file” using jQuery validation engine? 使用jQuery Validate插件进行表单验证 - Form validation with jQuery Validate plugin 使用jquery验证插件验证表单 - Validate form with jquery validation plugin jQuery基于单选来验证输入文本控件 - jQuery to Validate an Input Text Control based on Radio Selection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM