0

我一直在四处寻找,不明白为什么我的代码不起作用。甚至尝试过 jsfiddle 看看它是否有效,但对我来说没有发生。

为了清楚起见,我正在使用 twitter bootstrap,并且还没有对表单的 POST 部分做任何事情。

<div id="register">
    <form id="registerform" method="POST" action=""  accept-charset='UTF-8'>
       <fieldset>
         <legend>Register</legend> <br>
         <input type="text" name="firstName" id="firstName" placeholder="First Name" maxlength="20" value=""/> <br>
         <input class="btn btn-primary" type="submit" name="create" value="Join"/>
         <a href="/"><button type="button" class="btn">Cancel</button></a>
      </fieldset>
    </form>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js" type="text/javascript"></script>
<script src="../js/jquery.validation.min.js"></script>
<script>
 $(document).ready(function () {
               var form = $("#registerform");
               var firstname = $("#firstName");                      
               var validator = form.validate({
                        rules: {
                            firstname: {
                                required: true,
                                minlength: 4
                            }
                        },
                });
     });

任何帮助都会很棒。

4

2 回答 2

1

您的字段namefirstName,并且该rules选项采用name属性,而不是id您尝试执行的操作。

演示:http: //jsfiddle.net/xGc79/

$(document).ready(function () {

    var form = $("#registerform");

    var validator = form.validate({
        rules: {
            firstName: { // <-- this is the `name` attribute
                required: true,
                minlength: 4
            }
        }
    });

});
于 2013-04-30T17:03:50.057 回答
0

规则区分大小写

$(document).ready(function () {
    var form = $("#registerform");
    var firstname = $("#firstName");                      
    var validator = form.validate({
        rules: {
            firstName: {
                required: true,
                minlength: 4
            }
        },
    });

    form.submit(function(){
        if(!form.valid()){
            return false;
        }
    })
});

演示:小提琴

于 2013-04-30T16:56:35.717 回答