MVC 5-JQuery对dd/mm/YYYY的验证
我有一个MVC5应用程序,它使用实体框架SQL Server模型。我使用VisualStudio中的模板构建了它 该页面运行良好,但日期验证让我疲惫不堪 如果日期是向后的(例如yyyy/mm/dd或美国),则验证只允许我添加日期。我已经将英国文化添加到网络配置中,但这没有帮助。有没有一种方法可以用uk格式重写jquery,我应该把它放在我的代码中的什么地方 很抱歉,我对asp mvc和razor一无所知 我的代码如下:MVC 5-JQuery对dd/mm/YYYY的验证,jquery,asp.net-mvc,date,Jquery,Asp.net Mvc,Date,我有一个MVC5应用程序,它使用实体框架SQL Server模型。我使用VisualStudio中的模板构建了它 该页面运行良好,但日期验证让我疲惫不堪 如果日期是向后的(例如yyyy/mm/dd或美国),则验证只允许我添加日期。我已经将英国文化添加到网络配置中,但这没有帮助。有没有一种方法可以用uk格式重写jquery,我应该把它放在我的代码中的什么地方 很抱歉,我对asp mvc和razor一无所知 我的代码如下: @model messageBoard.Models.tblMessage
@model messageBoard.Models.tblMessage
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>tblMessage</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.MessageID)
<div class="form-group">
@Html.LabelFor(model => model.Sender, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Sender, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Sender, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Receiver, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Receiver, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Receiver, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.YearGroup, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.YearGroup, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.YearGroup, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Expiry, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Expiry, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Expiry, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@model messageBoard.Models.tblMessage
@{
ViewBag.Title=“编辑”;
}
编辑
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
TBL消息
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.HiddenFor(model=>model.MessageID)
@LabelFor(model=>model.Sender,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Sender,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Sender,“,new{@class=“text danger”})
@LabelFor(model=>model.Receiver,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Receiver,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Receiver,“,new{@class=“text danger”})
@LabelFor(model=>model.YearGroup,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.YearGroup,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.YearGroup,“,new{@class=“text danger”})
@LabelFor(model=>model.Date,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Date,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Date,“,new{@class=“text danger”})
@LabelFor(model=>model.Expiry,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Expiry,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Expiry,“,new{@class=“text danger”})
@LabelFor(model=>model.Title,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Title,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Title,“,new{@class=“text danger”})
@LabelFor(model=>model.Message,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Message,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Message,“,new{@class=“text danger”})
}
@ActionLink(“返回列表”、“索引”)
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
}
您可以使用jQuery重新配置验证器以使用应用程序中的当前区域性集,如下所示
$.validator.addMethod('date', function (value, element) {
var d = new Date();
return this.optional(element) || !/Invalid|NaN/.test(
new Date(d.toLocaleDateString(value)));
});
此方法必须位于具有日期输入字段的页面上。您需要将服务器上的区域性更改为以
dd/MM/yyyy
格式接受日期的区域性(例如在web.config
文件中)“我知道sql server以这种格式存储日期”。不,它没有,它使用一个内部表示,它与任何人类可读的格式无关。另外,验证规则在MVC中,与SQL无关。我同意这一切都与MVC验证有关,与SQL无关。不管怎样,您是否可以扩展“验证只允许我添加一个日期”-您正在做什么,发生了什么?(您看到了什么消息)关于这个糟糕的问题我的错,对不起,我是MVC新手。基本上我认为是jquery验证阻止了我输入英国格式dd/mm/yyyy的日期。但是我不知道如何更改。我将这一行添加到webconfig中。但是它没有帮助。还有其他想法吗?默认情况下,jquery.validate.js`基于M进行验证M/dd/yyyy
格式。如果您希望以dd/MM/yyyy
格式输入日期,并将(比如)2017年12月30日作为有效日期,则需要重新配置验证程序。