Jquery验证程序代码始终返回false

时间:2013-04-20 21:55:24

标签: jquery jquery-validate

我试图了解这个功能。但是每次我运行它时,它都告诉用户代码是不正确的,尽管我已经知道了。有人可以在这里暗示我的缺陷

Html就是

<form id="Join_league" method="post" action="<? echo $HTTP_SERVER_VARS['PHP_SELF'];?>">
    <input name="league_code" id="league_code" class="input">
                    <br><br>
    <input id="Join_button" name="Join_button" value="Enter League" type="submit"/>
</form>

这是我的javascript / ajax功能

          <script src="http://code.jquery.com/jquery-latest.js"></script>
           <script type="text/javascript" src="http://jzaefferer.github.com/jquery   validation/jquery.validate.js"></script>


    <script type="text/javascript">

      $(document).ready(function() {
$.validator.addMethod("valid_code",function(value,element){
    var parameter="";
    $.ajax({
          url: "check_league_code.php",
          type: "POST",
          async: false,
          data: parameter,

          success:function(output)
                 {
                    return true;
                 }
     });
});


$("#Join_league").validate({
    rules: {
    league_code: {number : true,required : true ,
                              valid_code: true
                                            }
},
messages: {
    league_code: { required:"<strong>Please enter code</strong>",
                  number:"<strong>Please enter number only</strong>",
                valid_code:"<strong>Sorry incorrect code entered</strong>"
        }

}

   })

});

</script>

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

这是我用来返回true或false的代码

<script type="text/javascript">
$(document).ready(function() {

    jQuery.validator.addMethod("valid_code", function(value,element) {
          var isSuccess = false;
          $.ajax({ url: "check_league_code.php",
          data: "",
          async: false,
          success: function(msg) { 
                      if(msg === "1")
                              {
                                isSuccess=true;
                              }
                    }
           });
                return isSuccess;
             },"<strong>Sorry incorrect code entered</strong>");



    $("#Join_league").validate({
        rules: {
            league_code: {
                required : true,
                number : true,
                valid_code: true
                        },
            messages: {
                league_code: {
                    required: "<strong>Please enter code</strong>",
                    number: "<strong>Please enter number only</strong>",
                    valid_code: "<strong>Sorry incorrect code entered</strong>"
                             }
                     }
        }
    });

});