简体   繁体   English

jQuery验证DIV上的插件

[英]jQuery validate plugin on DIV

I am trying to use the validate plugin on a div as shown in the answer to this question : 我试图在div上使用validate插件如此问题的答案所示:

<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>

I have all within a form. 我有一个表格。

I am getting a bunch of different errors on different browsers. 我在不同的浏览器上遇到了一堆不同的错误。

  • Firefox : validator in undefined Firefox :未定义的验证器
  • IE8 : 'settings' is null or not an object IE8 :'settings'为null或不是对象
  • Chrome : Cannot read property 'settings' of undefined Chrome :无法读取未定义的属性“设置”

Any help appreciated! 任何帮助赞赏!

This isn't the answer you want to hear, but the other answer is incorrect (it may have been right when it was posted , but there have been several major jQuery validation plugin changes since then). 这不是你想听到的答案,但另一个答案是不正确的(它可能在发布时是正确的,但从那以后有几个主要的jQuery验证插件更改)。

The validation plugin is (currently) designed to work on a <form> , and only on a <form> . 验证插件 (目前)设计用于<form>用于<form> You can also note that all of the plugin documentation references a form, not any other generic container. 您还可以注意到, 所有插件文档都引用了一个表单,而不是任何其他通用容器。

The plugin itself keeps track of validator.currentForm internally, which refers to the this of the passed in selector, getting .elements , etc off that...it's really not going to work any other way, not the way the current version's written. 该插件本身跟踪validator.currentForm内部,它指的是this在选择了通过,得到的.elements等关闭该......它真的行不通的任何其他方式,而不是当前版本的书写方式。

The overall solution/alternative approach here: call .validate() on the <form> element (the jQuery wrapper for it rather) directly, not any other container. 这里的整体解决方案/替代方法:直接<form>元素(相反的jQuery包装器.validate()上调用.validate() ,而不是任何其他容器。 If you need to divide your <form> use <fieldset> elements, possibly using the ignore: ':hidden' option in .validate() if you don't want to validate input fields that aren't visible to the user. 如果您需要划分<form> use <fieldset>元素,可能使用.validate()ignore: ':hidden'选项,如果您不想验证用户不可见的输入字段。

Open jquery.validate.js or jquery.validate.min.js and find(ctrl + F) " label " and replaceAll with your your required tag: 打开jquery.validate.jsjquery.validate.min.js并找到(ctrl + F)“ label ”并将replaceAll替换为您所需的标签:

Example: div 示例:div

then perform validation. 然后执行验证。

You're missing a closing bracket. 你错过了一个结束括号。 Try this instead: 试试这个:

$("#pseudoForm").validate({
    onfocusout:true,
    rules:{
        first_name:"required",
        last_name:"required"
    }
});
//HTML

<div class="form-group has-feedback col-xs-8 " style="padding-right:0px">
                <input type="tel" class="form-control" name="Otp_MobileNo" id="mobileNo" placeholder="Mobile No."  minlength="10" maxlength="10">
                <span id="mobileno_message"  style="display:none;color: red;">Please enter a valid Mobile No</span>
            </div>


    //Java Script
     $("#getOtp").click(function(){
         jQuery(document).ready(function(){





            var MobileNo = jQuery("#mobileNo").val();
            var MobileNoLength = MobileNo.length;  
            var zipRegex = /^\d{10}$/;
             var mobileNo = $("#mobileNo").val();

            if (!zipRegex.test(MobileNo))
            { 
               jQuery('#mobileno_message').show();


            }
            else
            { 

              // success!

                 jQuery('#mobileno_message').hide();
                 $.ajax({
                type : 'POST',
                url  : '<?php echo site_url('Login/send_otp'); ?>',
                data : {Otp_MobileNo:mobileNo,},
                dataType    : 'json',
                beforeSend: function()
                { 
                  $("#error").fadeOut();
                },
                success :  function(response)
                { 
                alert(response.message_text);
                $("#check-otp").delay().animate({
                    height: 'toggle',
                  },
                  "slow");
                $("#getOtp").hide();
                }
             });





            }

          });



           });

You can get the same error if you select the form by class 如果按类选择表单,则会出现相同的错误

$(".form_class").validate(...

instead of by ID $("#form_id").validate(... 而不是ID $("#form_id").validate(...

or tag name $("form").validate(... 或标记名称$("form").validate(...

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

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