无法弄清楚如何将 jquery.unobstrusive 应用于 jquery.ui.datepickers 以要求日期字段
使用 MVC3 Razor,我有一个包含一些文本的部分视图、两个文本框(开始日期和结束日期)、一个取消按钮和一个搜索按钮。
当在 StartDate 和 EndDate 文本框中输入无效日期时,文本框会突出显示红色,并带有日期选择器使用的不显眼的装饰?
我不知道如何要求这些字段并将它们突出显示为红色,就像无效数据输入一样。
这是我的代码
<script type="text/javascript">
$(document).ready(function () {
$('.date').datepicker({ dateFormat: "mm/dd/yy" });
$("#printnoteform").validate({
rules: {
startdate: {
required: true
},
enddate: {
required: true
}
},
messages: {
startdate: {
required: "Required"
},
enddate: {
reqquierd: "Required 2"
}
}
});
});
</script>
表单...
@using (Html.BeginForm("PrintNotes", "Note", FormMethod.Post, new { id = "printnoteform", name = "printnoteform" }))
{
......
StartDate & HTML EndDate
<input type="text" id="startdate" name="startdate" class="date" />
<input type="text" id="enddate" name="enddate" class="date" />
我没有浏览器错误,StartDate 和 EndDate 不是模型的一部分,如果不需要的话我不想制作一个。 Web.config 设置正确。
我缺少什么?
Using MVC3 Razor, I have a partial view with some text, two text boxes (StartDate & EndDate), a Cancel and a Search button.
When invalid dates are entered into the StartDate and EndDate textboxes, the textboxes highlight red with the unobstrusive decoration the datepicker uses?
I cannot figure out how to require these fields and highlight them red as the invalid data entry does.
Here is my code
<script type="text/javascript">
$(document).ready(function () {
$('.date').datepicker({ dateFormat: "mm/dd/yy" });
$("#printnoteform").validate({
rules: {
startdate: {
required: true
},
enddate: {
required: true
}
},
messages: {
startdate: {
required: "Required"
},
enddate: {
reqquierd: "Required 2"
}
}
});
});
</script>
The form...
@using (Html.BeginForm("PrintNotes", "Note", FormMethod.Post, new { id = "printnoteform", name = "printnoteform" }))
{
......
HTML for StartDate & EndDate
<input type="text" id="startdate" name="startdate" class="date" />
<input type="text" id="enddate" name="enddate" class="date" />
I have no browser errors, StartDate and EndDate are not part of a model and I don't want to make one if I don't have to. Web.config is setup properly.
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有两种相互排斥的可能性:
如果您想使用 jquery unobtrusive,则不应手动添加任何
.validate
规则到你的表格(就像你所做的那样)。这是由 jquery unobtrusive 脚本自动完成的。当然,为了能够做到这一点,您的输入字段应该使用 HTML5 data-* 属性进行修饰,以指示要使用的规则和消息。例如:通常,这是当您使用视图模型并绑定到其属性时标准 Html 帮助程序(例如 EditorFor)生成的输出。他们在视图模型上使用数据注释来推断验证规则并将它们转置为 data-* 属性。
第二种可能性是手动连接验证规则。在这种情况下,您应该删除 jquery 不引人注目的脚本的任何痕迹,因为它将与表单上的
.validate
规则发生冲突。在这种情况下,由您来定义规则和消息。You have two mutually exclusive possibilities:
If you want to use jquery unobtrusive you shouldn't be manually adding any
.validate
rules to your form (as you did). This is done automatically by the jquery unobtrusive script. Of course for it to be able to do it your input fields should be decorated with HTML5 data-* attributes in order to indicate the rules and messages to use. For example:Normally this is the output that standard Html helpers such as EditorFor generate when you use a view model and bind to its properties. They use Data Annotations on your view model to infer the validation rules and transpose them as data-* attributes.
The second possibility is to manually hook up the validation rules. In this case you should remove any traces of the jquery unobtrusive script as it will conflict with your
.validate
rules on the form. It is up to you in this case to define the rules and messages.