javascript 多步表单上的jquery验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20866607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:29:52  来源:igfitidea点击:

jquery Validation on Multi Step Form

javascriptjqueryjquery-validate

提问by SCC

I have this multi-step form which I would like to validate (to make sure video file with correct format is specified and all the required field are filled up). I want to use jquery.validate plugin to achieve this.

我有这个多步骤表单,我想对其进行验证(以确保指定了格式正确的视频文件并填写了所有必填字段)。我想使用 jquery.validate 插件来实现这一点。

The problem is that the "Upload" button was working fine before I added in this validation code. I believe something is wrong with my code which causes the "Upload" button to stop working. I have included a fiddle here: http://jsfiddle.net/NZr4n/

问题是在我添加此验证代码之前“上传”按钮工作正常。我相信我的代码有问题导致“上传”按钮停止工作。我在这里包含了一个小提琴:http: //jsfiddle.net/NZr4n/

I believe this is the part which causes the error. Anyone could help?

我相信这是导致错误的部分。任何人都可以帮忙吗?

$('#msform').validate({
            rules: {    
                videoFile: {
                    required: true,
                    extension:'mov|mp4|mpeg|wmv'
                }
                videoTitle: {
                    required: true,
                }
                videoDescription: {
                    required: true,
                }
            },
            messages: {
                videoFile: "Please specify a file",
                videoTitle: "Title is required"
                videoDescription: "Description is required"
            }
        });

        if ((!$('#msform').valid()) {
            return false;
        }

回答by Josh Beam

I went through your code on jsfiddle, and I noticed some seemingly minor syntax issues, which had major effects. Here is the corrected version here (and here's a link to the working jsfiddle):

我在 jsfiddle 上浏览了你的代码,我注意到一些看似很小的语法问题,但产生了重大影响。这是更正后的版本(这里是工作jsfiddle的链接):

    $('#msform').validate({
        rules: {    
            videoFile: {
                required: true,
                extension:'mov|mp4|mpeg|wmv'
            }, //notice the comma I added
            videoTitle: {
                required: true,
            }, //notice the comma I added
            videoDescription: {
                required: true,
            }
        },
        messages: {
            videoFile: "Please specify a file", //notice the comma I added
            videoTitle: "Title is required", //notice the comma I added
            videoDescription: "Description is required"
        }
    });

    if ((!$('#msform').valid())) { //I added an extra parenthesis at the end
        return false;
    }

Most major browsers now have options for developer consoles.

大多数主要浏览器现在都有用于开发者控制台的选项。

In Firefox, you can download Firebug. I opened Firebug up while looking at your jsfiddle, and it immediately showed me all the syntax errors. That's how I corrected the above code.

在 Firefox 中,您可以下载 Firebug。我在查看您的 jsfiddle 时打开了 Firebug,它立即向我显示了所有语法错误。这就是我更正上述代码的方式。

Google Chrome also has a console, under View -> Developer -> JavaScript Console

Google Chrome 也有一个控制台,在 View -> Developer -> JavaScript Console 下

Just remember that members in an object have to be separated by commas. That was what caused the majority of your syntax issues.

请记住,对象中的成员必须用逗号分隔。这就是导致您的大部分语法问题的原因。