Jquery日期选择器返回无效的日期消息
我正在尝试启动并运行一个应用程序的出生日期字段。首先,我创建了一个包含以下内容的模型 模型 接下来是控制器,这是一个简单的创建 控制器Jquery日期选择器返回无效的日期消息,jquery,asp.net-mvc,jquery-ui-datepicker,Jquery,Asp.net Mvc,Jquery Ui Datepicker,我正在尝试启动并运行一个应用程序的出生日期字段。首先,我创建了一个包含以下内容的模型 模型 接下来是控制器,这是一个简单的创建 控制器 // POST: Customers/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com
// POST: Customers/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CustomerId,FirstName,MiddleName,Surname,DOB,Gender,Address,AddressL2,Town,Postcode,Phone,Email")] Customer customer)
{
if (ModelState.IsValid)
{
//Import the libphoneNumber class
PhoneNumberUtil phoneUtil = PhoneNumberUtil.GetInstance();
PhoneNumber n = phoneUtil.Parse(customer.Phone, "GB");
customer.PhoneNumberFormatted = phoneUtil.Format(n, PhoneNumberFormat.INTERNATIONAL);
customer.CustomerId = Guid.NewGuid();
db.Customers.Add(customer);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(customer);
}
最后是我的视图,其中Jquery运行以使日期选择器工作
@model Phase5.Models.Customer
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Customer</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MiddleName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MiddleName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MiddleName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Surname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Surname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Surname, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DOB, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DOB, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DOB, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.Gender, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AddressL2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AddressL2, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AddressL2, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Town, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Town, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Town, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Postcode, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Postcode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Postcode, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script>
$("#DOB").datepicker({
dateFormat: 'd/mm/yy',
changeMonth: true,
changeYear: true,
firstDay: 1,
minDate: Date.parse("1900-01-01"),
maxDate: Date.parse("2100-01-01"),
yearRange: "c-90:c+150"
});
// validation in case user types the date out of valid range from keyboard : To fix the bug with out of range date time while saving in sql
$(function () {
$.validator.addMethod(
"date",
function (value, element) {
var minDate = Date.parse("1900-01-01");
var maxDate = Date.parse("2100-01-01");
var valueEntered = Date.parse(value);
if (valueEntered < minDate || valueEntered > maxDate) {
return false;
}
return !/Invalid|NaN/.test(new Date(minDate));
},
"Please enter a valid date!"
);
});
</script>
}
@model Phase5.Models.Customer
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
顾客
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.FirstName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.FirstName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.FirstName,“,new{@class=“text danger”})
@LabelFor(model=>model.MiddleName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.MiddleName,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.MiddleName,“,new{@class=“text danger”})
@LabelFor(model=>model.姓氏,htmlAttributes:new{@class=“controllabel col-md-2”})
@Html.EditorFor(model=>model.namname,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.namname,“,new{@class=“text danger”})
@LabelFor(model=>model.DOB,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.DOB,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.DOB,“,new{@class=“text danger”})
@LabelFor(model=>model.Gender,htmlAttributes:new{@class=“controllabel col-md-2”})
@EnumDropDownListFor(model=>model.Gender,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Gender,“,new{@class=“text danger”})
@LabelFor(model=>model.Address,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Address,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Address,“,new{@class=“text danger”})
@LabelFor(model=>model.AddressL2,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.AddressL2,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.AddressL2,“,new{@class=“text danger”})
@LabelFor(model=>model.Town,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Town,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Town,“,new{@class=“text danger”})
@LabelFor(model=>model.Postcode,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Postcode,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Postcode,“,new{@class=“text danger”})
@LabelFor(model=>model.Phone,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Phone,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Phone,“,new{@class=“text danger”})
@LabelFor(model=>model.Email,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Email,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Email,“,new{@class=“text danger”})
}
@ActionLink(“返回列表”、“索引”)
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
$(“#DOB”).日期选择器({
日期格式:“d/mm/yy”,
变化月:对,
变化年:是的,
第一天:1,
minDate:Date.parse(“1900-01-01”),
maxDate:Date.parse(“2100-01-01”),
年份范围:“c-90:c+150”
});
//如果用户从键盘键入的日期超出有效范围,则进行验证:在保存sql时,使用超出范围的日期时间修复错误
$(函数(){
$.validator.addMethod(
“日期”,
函数(值、元素){
var minDate=日期解析(“1900-01-01”);
var maxDate=Date.parse(“2100-01-01”);
var valueEntered=Date.parse(值);
如果(输入的值maxDate){
返回false;
}
返回!/Invalid | NaN/.test(新日期(minDate));
},
“请输入有效日期!”
);
});
}
我遇到的问题是,我用于测试的大多数日期返回时都有错误
@model Phase5.Models.Customer
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Customer</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MiddleName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MiddleName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MiddleName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Surname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Surname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Surname, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DOB, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DOB, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DOB, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.Gender, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AddressL2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AddressL2, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AddressL2, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Town, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Town, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Town, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Postcode, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Postcode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Postcode, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script>
$("#DOB").datepicker({
dateFormat: 'd/mm/yy',
changeMonth: true,
changeYear: true,
firstDay: 1,
minDate: Date.parse("1900-01-01"),
maxDate: Date.parse("2100-01-01"),
yearRange: "c-90:c+150"
});
// validation in case user types the date out of valid range from keyboard : To fix the bug with out of range date time while saving in sql
$(function () {
$.validator.addMethod(
"date",
function (value, element) {
var minDate = Date.parse("1900-01-01");
var maxDate = Date.parse("2100-01-01");
var valueEntered = Date.parse(value);
if (valueEntered < minDate || valueEntered > maxDate) {
return false;
}
return !/Invalid|NaN/.test(new Date(minDate));
},
"Please enter a valid date!"
);
});
</script>
}
<globalization uiCulture="en" culture="en-GB" />