Jquery验证插件 - TypeError:$(...)。validate不是函数

问题描述 投票:33回答:6

我的脚本抛出错误:

TypeError:jQuery.validator未定义additional-methods.js:20 TypeError:$(...)。validate不是函数index.php:115

可能,我在jQuery代码中有错误。

<head>
<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<script src="http://jquery.bassistance.de/validate/additional-methods.js"></script>
</head>
<body>
            <form id="registerForm" method="post" action="logrej.php">
            <input name="login" type="text"/>
            <input name="nick" type="text"/>
            <input type="password" id="passw" name="password"/>
            <input type="password" name="retype" />
            <input type="submit" value="Zarejestruj!" />
            </form>
            <script>

                $("#registerForm").validate({
                    rules: {
                        login: {
                            required:true,
                            rangelenght: [4,20],
                            remote:"look.php"
                        },
                        nick : {
                            required:true,
                            rangelenght:[4,20],
                            remote:"look.php"
                        },
                        password: {
                            required:true,
                            rangelenght:[4.20]
                        },
                        retype: {
                            required:true,
                            equalTo:"#passw"
                        }
                    },
                    messages:{
                        login:{
                            required:"To pole jest wymagane!"
                        }
                    }
                })

            </script>
javascript jquery jquery-validate
6个回答
76
投票

您没有加载验证插件。你需要:

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>

将它放在加载其他方法的行之前。

此外,您还应该从CDN获取其他方法,而不是jquery.bassistance.de。

其他错误:

[4.20]

应该

[4,20]

rangelenght:

应该:

rangelength:

2
投票

对我来说,通过将http://ajax改为https://ajax来解决问题......(在http中添加一个S)

https://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js

1
投票

它看起来像你得到的JavaScript错误可能是由

password: {
    required:true,
    rangelenght:[4.20]
},

因为[4.20]应该是[4,20],我猜这是在additional-methods丢掉验证码,因此给出了你发布的类型错误。

编辑:正如其他人在下面的评论中指出的那样rangelenght也拼错了,jquery.validate.js库似乎丢失了(假设它没有编译到你的其他资产中)


1
投票

我有同样的问题。我使用jquery验证作为npm模块,我的修复是在我的js文件的开头要求模块:

require('jquery-validation');

0
投票

jquery.validate.js之前加入additional-methods.js

那里定义了$.validate()方法


0
投票

您没有包含基本jQuery验证库:

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script>

把它放在附加方法库之前。 (BTW这是一个托管版本,如果你想下载你自己的版本)

© www.soinside.com 2019 - 2024. All rights reserved.