jQuery Validation插件已经是jQuery的一部分了?

时间:2014-03-31 11:03:57

标签: javascript jquery jquery-validate

我的jquery:

$("#sample_form").validate({
    rules: {
        name: "required"
    },
    messages: {
        name: "This field is required."
    }
});

形式:

<?php   $form_att = array('id' => 'sample_form');
        echo form_open('Controller/function',$form_att); ?>
      <input type='text' name='name' id='name'/>
    <?php echo form_close(); ?>

我做得对吗?名称字段不应为空。虽然不起作用。我需要下载另一个jquery插件吗?我下载了jquery。虽然这需要一个特定的jquery,如游戏有mods / expansions。不完全确定,但我想做客户端验证。

编辑:

添加验证jquery后,其他功能无法正常工作。如果我拿出验证的代码片段,其他人再次工作。

我有按钮来追加输入类型。

示例,

$("#add").click(function(){
        $("#sample_td").append("<p><input type='text' id='sample[]' name='sample[]' size='60' placeholder='Instruction line'/></p>");
    });

如果单击带有id add的按钮,则会在TD内部附加一个id为sample_td的输入类型。

id中的[]是对所有附加的输入类型进行分组,所以如果我想验证是否需要样本,并且附加了多少个,那么它将是

规则:{sample []:&#34;必需&#34; },messages:{sample []:&#34;此字段是必填字段。&#34; }

因此,如果将验证任何输入类型的样本。我从与将名称字段添加[]相同的功能中获得了这个想法,所以如果i $ _POST [sample&#39;];它会返回所有字段的所有值,名称为&#34; sample []&#34;作为一个数组。

4 个答案:

答案 0 :(得分:1)

我在这里找到了一个不错的jquery验证插件 http://bassistance.de/jquery-plugins/jquery-plugin-validation/

或者您可以使用html5验证。 <input type='text' name='name' id='name' required/>

答案 1 :(得分:0)

您需要加载jQuery和jQuery Validation Plugin。

jQuery Validation Plugin可在jqueryvalidation.org

获得

答案 2 :(得分:0)

引用OP评论:

  

&#34;我想点击下载jquery会给我整个包。&#34;

没有&#34;包&#34;。 jQuery是一个JavaScript框架。 (它是一个单独的JavaScript文件,没有别的。它不可能包含地球上的每个插件。)Refer to jQuery API documentation to see the only methods that are provided

jQuery Validate第三方 jQuery 插件,而不是jQuery内置的方法。 (可能millions of third-party jQuery plugins。)


您必须始终首先包含jQuery ,然后再包含任何第三方插件。

<script type="text/javascript" src="your-path-to/jquery.min.js"></script>
<script type="text/javascript" src="your-path-to/jquery.validate.min.js"></script>

从CDN here下载或热链接到jQuery验证v1.11.1。

你只需要使用缩小的OR或未缩小版本的插件......不是两者兼而有之。除非您正在修改代码或想要阅读代码,否则只需使用缩小版本。


然后只有之后包含jQuery和jQuery插件,你就会包含自己的jQuery代码。

您的代码看起来对于您提供的HTML标记是正确的,但是您应该将其包含在DOM ready事件处理程序中,以确保在您的函数被触发之前HTML已准备就绪。

<script type="text/javascript">
    $(document).ready(function() {  // ensures HTML markup has been constructed

        $("#sample_form").validate({
            rules: {
                name: "required"
            },
            messages: {
                name: "This field is required."
            }
        });

    });
</script>

答案 3 :(得分:-1)

如果问题始终是需要或不需要该字段,您可以使用优雅的解决方案。这适用于最新的浏览器。

http://www.the-art-of-web.com/html/html5-form-validation/