结合“简单”不引人注目的 jQuery 验证
当页面包含 jquery.validate.unobtrusive.js 时,如何向我的 MVC3 表单添加额外的 jQuery 验证规则?
我在表单底部有一些“手动”创建的元素,我想向其中添加一些简单的客户端验证,但是加载了不显眼的脚本后,以下内容不起作用,即归档不验证。
$("#myForm").validate({
rules: {
"broadcast-blockSize": {
required: true,
number: true
}
}
How can I add additional jQuery Validate rules to my MVC3 form when the page includes jquery.validate.unobtrusive.js?
I have some 'manually' created elements at the bottom of the form that I would like to add some simple client-side validation to, but with the unobtrusive script loaded, the following does not work, i.e. the filed does not validate.
$("#myForm").validate({
rules: {
"broadcast-blockSize": {
required: true,
number: true
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管我对客户端代码缺乏经验,但我正在帮助我的同事查找一些指针。我找到了很多链接,但对他不起作用(可能是因为大多数是MVC3,而他正在使用MVC4)。
然而,他找到了一种在 MVC3 中实现这一点的方法,所以我将发布该代码。我不知道它是否适用于 MVC3(OP 要求),但我希望它会对某人有所帮助。
在对大多数字段使用不显眼的验证时,我的同事使用类似于 EditorTemplate 底部的代码(对于 Partialview)添加了一条规则:
Even though I have little experience with client-side code, I was helping my colleague looking up some pointers. I found many links, but they did not work for him (maybe because most were MVC3, and he is using MVC4).
However, he figured out a way to do it in MVC3, so I'll just post that code. I don't know if it will work for MVC3 (requested by OP), but I hope it will help someone.
While using unobtrusive validation for most fields my colleague adds a rule using code similar to the code underneath at the bottom of an EditorTemplate (for a Partialview):