简体   繁体   中英

jQuery: validate rules not working as expected

<script type="text/javascript">
    $(document).ready(function () {
        $("#loginForm").validate({
            rules: {
                email: {
                    required: true,
                    minlength: 10
                },
                passwd: {
                    required: true,
                    minlength: 8
                }
            },
            messages: {
                email: {
                    required: "Please enter your email",
                    minlength: "Minlength has to be 10"
                },
                passwd: {
                    required: "Please enter your email",
                    minlength: "Minlength has to be 8"
                }
            },
            submitHandler: function (form) {
                alert('valid form submit');
            }
        });
    })
</script>

I have this in the HTML head_

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js"></script>

And the jQuery above at the end before closing body tag.

Following code works perfectly fine on jsfiddle when I use http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js and http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js .

but on my browser, it always goes to submit handler. http://jsfiddle.net/4937t/

I really appreciate your time.

I am not sure how did you include the scripts in your html document but let me show you the working example. It works on my chrome. I guess the position of your scripts is misplaced. Hope this works

<!doctype html>
<html>
<body>
   <form id="loginForm">
     <input name="email" type="text"/><br/>
     <input name="passwd" type="password"/><br/>
     <input type="submit" />
   </form>
   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
   <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js"></script>
   <script>
    $(document).ready(function () {
        $("#loginForm").validate({
            rules: {
                email: {
                    required: true,
                    minlength: 10
                },
                passwd: {
                    required: true,
                    minlength: 8
                }
            },
            messages: {
                email: {
                    required: "Please enter your email",
                    minlength: "Minlength has to be 10"
                },
                passwd: {
                    required: "Please enter your password",
                    minlength: "Minlength has to be 8"
                }
            },

            submitHandler: function (form) {
                alert('valid form submit');
            }
        });
    });
  </script>
  </body>
  </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM