如何在我的注册表中使用jquery验证 [英] How to use jquery validation in my registration form

查看:47
本文介绍了如何在我的注册表中使用jquery验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的就是如果有人单击创建"按钮,则在我的整个注册表单中执行jquery验证.

I want to do is perform a jquery validation in my entire registration form if someone click the create button.

我的问题是我单击按钮后没有任何反应.有人可以帮我吗?

My problem is after i click the button nothing really happens. Can someone help me with this?

当前输出: http://jsfiddle.net/5kcsn/3/

$(document).ready(function () {
    $('#create').click(function () {
        $('form').validate({
            rules: {
                fname: {
                    minlength: 3,
                    maxlength: 15,
                    required: true
                },
                lname: {
                    minlength: 3,
                    maxlength: 15,
                    required: true
                },
                email: {
                    required: true,
                    email: true
                },
                reemail: {
                    required: true,
                    email: true
                },
                password: {
                    minlength: 6,
                    maxlength: 15,
                    required: true
                },
                gender: {
                    required: true
                },
                month,
                day,
                year: {
                    required: true
                }
            },
            highlight: function (element) {
                $(element).closest('.form-group').addClass('has-error');
            },
            unhighlight: function (element) {
                $(element).closest('.form-group').removeClass('has-error');
            },
            errorElement: 'span',
            errorClass: 'help-block',
            errorPlacement: function (error, element) {
                if (element.parent('.input-group').length) {
                    error.insertAfter(element.parent());
                } else {
                    error.insertAfter(element);
                }
            }
        });
    });
});

推荐答案

您遇到三个问题.

1)rules选项中的格式无效.此选项的参数只能是key:value对.您不能使用逗号分隔的字段名称列表代替单个key.

1) Invalid format within your rules option. The parameters for this option can only be key:value pairs. You cannot use a comma separated list of field names in place of a single key.

month,  // <- key is missing value.  key:value
day,    // <- key is missing value.  key:value
year: {
    required: true
}

应该是...

month: {
    required: true
},
day: {
    required: true
},
year: {
    required: true
}

2)您没有将.validate()方法放置在处理程序中. .validate()只是插件的 initialization 方法,并且在DOM ready事件处理程序中被调用一次.

2) You've improperly placed the .validate() method within a click handler. .validate() is only the initialization method of the plugin, and gets called once within a DOM ready event handler.

3)您的<button>type="button",插件将始终将其忽略.将其更改为type="submit",以便自动捕获.

3) Your <button> is a type="button", which will always be ignored by the plugin. Change it to type="submit" so that it's automatically captured.

注意:我注意到您有两个不同的DOM ready事件处理程序.这是没有必要的.所有代码都可以放置在单个DOM ready处理程序中.

Note: I noticed you have two different DOM ready event handlers. This is not necessary. All code can be placed within a single DOM ready handler.

更新后的演示: http://jsfiddle.net/5kcsn/8/

Updated DEMO: http://jsfiddle.net/5kcsn/8/

这篇关于如何在我的注册表中使用jquery验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆