jQuery验证 - 不验证

时间:2009-09-04 15:56:50

标签: javascript jquery asp.net-mvc validation

我正在构建我的第一个ASP.net MVC应用程序(不是我的第一个jQuery和jQuery Validation插件应用程序),我在使用验证插件执行客户端验证时遇到了很糟糕的时间。有没有人知道使用VS 2008附带的jQuery-1.3.2.min插件是否有问题?

我的代码如下:

var v = $("#frmAccount").validate({
                rules: {
                    txtAccount: { minLength: 5,required: true  },
                    txtFName: "required",
                    txtLName: "required"
                },
                message: {               
                    txtAccount: {
                        required: "Account Number is required.",
                        minLength: jQuery.format("Account Number must be {0} or more charaters.")
                    },
                    txtFName: "First Name is required.",
                    txtLName: "Last Name is required.",
                }
            }); //validate
    $("#cmd").click(function() {
        if (v.form()) {
            $("#frmAccount").submit(); 
        } 
    });

............................
    < form method =“post”action =“/ Home / checkAccount”id =“frmAccount”>
< fieldset>
< legend>帐户信息< / legend>
< p>

< br />
< label for =“txtAccount”>帐号:< / label>
< br />
< input id =“txtAccount”name =“txtAccount”type =“text”value =“”/>




< br />
< label for =“txtFName”>名字:< / label>
< br />
< input id =“txtFName”name =“txtFName”type =“text”value =“”/>




< br />
< label for =“txtLName”>姓氏:< / label>
< br />
< input id =“txtLName”name =“txtLName”type =“text”value =“”/>




< br />
< label for =“txtLName”>电子邮件:< / label>
< br />
< input id =“txtEmail”name =“txtEmail”type =“text”value =“”/>




< / p>
< p>
< input type =“button”id =“cmd”name =“cmd”value =“Add”/>
< / p>
< / fieldset>
< / form>

当我尝试提交空白表单时,它会返回帐户旁边的默认“此字段是必填项”,fname和& lname字段,但如果我在字段中输入任何数据,我会得到各种结果。

我试图只执行基本验证功能(不将验证对象返回到v变量),但这产生了类似的结果。

我在这里忽略了一些愚蠢的事情吗?

2 个答案:

答案 0 :(得分:5)

Adrian Grigore,感谢您的快速回复。但是,我认为您可能忽略了上面发布的代码或其他内容。 也许你太忙于尝试插入自己的博客或其他东西。 IDK。

REAL问题如下:

*在上面的代码中,“消息”应该是“消息 s ” *另外,我做了(我考虑的)基本的MS编程背景错误...我的工作是minLength,当然,在jQuery中它应该是 minlength

阿德里安,祝你好运!

答案 1 :(得分:0)

jquery.validate可以正常使用jquery 1.32。 “各种结果”是什么意思?

在任何情况下,由于您使用的是ASP.NET MVC,我建议不要手动编写客户端验证代码。如果您使用xVal和DataAnnoationModelBinder,它会更容易,更稳定并且完全可测试。请查看client-side validation with ASP.NET MVC上的这篇文章。

相关问题