jQuery验证功能不起作用 [英] jQuery Validate function not working

查看:69
本文介绍了jQuery验证功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题,我敢肯定是很琐碎的,但我无法克服.

This problem, i am sure is very trivial but i am not able to get past it.

我的表单未使用jquery validate进行验证.

My form is not getting validated using jquery validate.

        $(document).ready(function () {
            $("#loginForm").validate({
                rules: {
                    email: "required",
                    passwd: "required",
                    email:{
                        minlength: 10
                    }
                },
                messages: {
                    email: "Please enter your email",
                    passwd: "Please enter your passwd",
                    email:{
                        minlength: "Minlength has to be 10"
                    }
                },

                submitHandler: function(form) {
                    $("#pp").text("right");
                    form.submit();
                }
            }); 
        })  

无控制台错误.我有

name,passwd是名称,我要验证的字段的ID,loginForm是表单的ID.问题是我通过无效输入时看不到错误消息.进入调试模式,尽管调用了validate之前ready函数内部的警报,但我什至看不到堆栈到达validate函数内部.

name, passwd are name, id of the fields i am trying to validate and loginForm is id of form. The problem is i don't see the error messages when i pass invalid input. Going through debug mode, i don't see the stack reaching inside validate function ever although an alert inside ready function before validate is called.

推荐答案

您没有显示HTML标记,因此此答案假定您正确命名了input元素.

You did not show your HTML markup, so this answer assumes you correctly named your input elements.

在您的代码中,您两次指定email ...

In your code, you're specifying email twice...

rules: {
    email: "required",
    passwd: "required",
    email:{  // <- duplicate
        minlength: 10
    }
},

如果您需要为一个字段指定多个规则,请指定一次一次并在其中列出规则...

If you need to specify multiple rules for one field, then specify the field once and list the rules inside...

rules: {
    email: {
        required: true,
        minlength: 10
    },
    passwd: "required"
},

您的messages选项有相同的问题.

Your messages option has the same problem.

工作演示: http://jsfiddle.net/4937t/

Working DEMO: http://jsfiddle.net/4937t/

这篇关于jQuery验证功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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