无法执行jquery验证

时间:2014-01-23 04:46:50

标签: php

我正在开发一个php网站,我需要jquery验证。我正在使用验证器。我已经加载了jquery文件和validate.js文件并将其放在我的本地文件夹中,但我无法执行验证。

错误讯息为"Uncaught TypeError: Cannot call method 'setDefaults' of undefined "

我陷入了这一步。请帮帮我。

Jquery代码。

<script type="text/javascript" src="js/jquery.validate.js"></script>
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script>
    $(function () {


       jQuery.validator.setDefaults({
           debug: true,
           success: "valid"
       });
       $("#Myform").validate({
           rules: {
               inputEmail: {
                   required: true,
                   minlength: 2

               }

           }

       });
    });
</script>

HTML code:

<form id="Myform" method="get" action="">
<div class="content">
      <form class="form-horizontal">
  <div class="control-group">
    <label class="control-label" for="inputEmail">Username</label>
    <div class="controls">
      <input type="text" id="inputEmail" name="inputEmail" placeholder="username">
    </div>
  </div>

3 个答案:

答案 0 :(得分:0)

jQuery应该包含在jQuery验证插件之前

<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/jquery.validate.js"></script>
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>

答案 1 :(得分:0)

1)只使用一个jquery.validate.js
2)jquery.validate.min.js只是jquery.validate.js的缩小版本

代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
        </script>
    <script type="text/javascript" src="jquery.validate.js">
    </script>
        <script>
    $(function () {$("#submit").click(function ()  //input button click
        {
             $.validator.setDefaults({
                   debug: true,
                   success: "valid" 
               });
               $("#Myform").validate({
                   rules: {
                       inputEmail: {
                           required: true,
                           minlength: 2

                       }

                   }

               });
            }); 
            }); 
        </script>

    <form id="Myform" method="POST" action="">
    <div class="content">

      <div class="control-group">
        <label class="control-label" for="inputEmail">Username</label>
        <div class="controls">
          <input type="text" id="inputEmail" name="inputEmail" placeholder="username">
           <input type="submit" id="submit" name="submit" placeholder="submit"> 
        </div>
      </div>
      </div>
      </form>

答案 2 :(得分:0)

<html>
  <body>
    <form id="Myform" method="get" action="">
        <div class="content">
            <form class="form-horizontal">
                <div class="control-group">
                    <label class="control-label" for="inputEmail">Username</label>
                    <div class="controls">
                    <input type="text" id="inputEmail" name="inputEmail" placeholder="username">
                    </div>
                </div>
            </form>
            <input type="submit" value="Submit">
        </div>
    </form> 
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.1/jquery.validate.min.js"></script>
    <script>
        $(function () {


           jQuery.validator.setDefaults({
               debug: true,
               success: "valid"
           });
           $("#Myform").validate({
               rules: {
                   inputEmail: {
                       required: true,
                       minlength: 2

                   }

               }

           });
        });
    </script>
</body>