为什么我不能使用jquery验证?

时间:2014-10-02 19:08:32

标签: javascript jquery validation

我正在使用jquery validate插件,但以下代码不起作用 - 我输入的任何内容都有效。

我花了很多时间试图解决这个问题,而且我已经修改了代码只有一个样本。

“billtoFirstName”应该有2到5个字符,当我输入5或1个字符时,不应该验证。我没有得到任何错误,提交显示我输入的任何内容都会传递给提交。我很难过。如何修复它以进行验证?

   <!DOCTYPE html>
   <html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js">          </script>
        <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js"></script>
        <style>
            #myTestForm{
                font-size:20px;
                border-color:black;
                border-style:solid;}
            #orderContactFormButton{
              background-color: aqua;
              float:right;
              font-size: 14px;
              font-weight:bold;
            }
            #orderContactBillTo{}
        </style>
<script>
$( document ).ready(function(){
$("#myTestForm").submit(function(event){// for testing on submit
       event.preventDefault(); // stop form from submitting normally
    var sv =  $("#billToFirstName").val();
       alert(" submit button here first = " + sv);
    });//submit

//validation options
    $("#myTestForm").validate({
        rules: {
           billToFirstName:{
               required:true,
                minlength: 2,
                maxlength: 5

            }
        },
        messages: {
           billToFirstName:{
            required:"Please enter your first name",
                minlength: "At least two characters are required",
                maxlength: "At most 5 characters are allowed"

            }
        }

        });//validate options for myTestform
});//document ready
</script>
    </head>
<body>
<form id ="myTestForm">
            <label for="billToFirstName" >First Name*:</label>
            <input name ="biilToFirstName" id ="billToFirstName" size = 50>
                       <br>
            <input id ="orderContactFormButton" type="submit"  value="Submit"><br>
        </form>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

有一个拼写错误:

<input name ="biilToFirstName" id ="billToFirstName" size = 50>  

只需解决这个问题,你就可以了......

  <input name="billToFirstName" id="billToFirstName">