无法在 validate.js 中调用 exactlength 属性

Unable to call exactlength property in validate.js

我正在尝试使用 jquery.validate.js 验证我的 html 文件。我需要验证 mobileno 字段的长度,它应该是 10 个字符。这是我的代码:

<!-- mobile number goes here. -->
                <div class="form-group">
                    <label for="mobileno" class="col-sm-2 control-label">Mobile No.</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" id="mobileno" name="mobileno" placeholder="Mobile No (10 digits)" required autofocus>
                    </div>
                    <span id="mobileno_error"></span>
                </div> 

对应的js文件为:

 $("#register_form").validate({
        // Specify the validation rules
        rules: {
            name: "required",
            email: {
                required: true,
                email: true
            },
            mobileno: {
                required: true,
                exactlength: 10
            },
            password: {
              required: true,
              minlength: 6
          },
          confirm_password:{
            required: true,
            equalTo: '#password'
        }
    },

        // Specify the validation error messages
        messages: {
            name:"Please enter your name.",
            email: {
                required: 'Email address is required',
                email: 'Please enter a valid email address',
            },
            mobileno: {
                required: 'Mobile number is required',
                exactlength: 'The mobile number should be of 10 characters.',
            },
            password: {
                required: 'Password is required',
                minlength: 'The password should be of minimum 6 characters.',
            },
            confirm_password: {
                required: 'Confirm password is required',
                equalTo: 'The confirm password should match password.',
            }

        },

在离开 mobileno 字段时,我遇到了如下问题:

jquery.validate.js:4 Uncaught TypeError: Cannot read property 'call' of undefined. Exception occurred when checking element mobileno, check the 'exactlength' method.

我不知道我做错了什么。

"I don't know what am I doing wrong?"

这个错误告诉你你做错了什么...

Cannot read property 'call' of undefined ... check the 'exactlength' method.

There is no such jQuery Validate rule called exactlength。您不能仅仅通过在 rules 对象中写入单词来制定新规则。

要强制一个字段的长度恰好是 X 个字符,您可以同时使用 minlengthmaxlength 规则并将它们都设置为 X。

mobileno: {
    required: true,
    minlength: 10,
    maxlength: 10
},

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

否则,the additional-methods.js file 中有一个 phoneUS 规则,您可以使用它来验证任何美国 phone 号码。另请参阅 mobileUKphoneUKphonesUKphoneNL 了解更多 phone 号码规则。

如果这不令人满意,那么您可以使用 the .addMethod() method.

编写自己的规则
rangelength: [10, 10]

这是字段长度恰好为X个字符的函数。必须加additional-method.js否则不支持