My MVC3 website uses jQuery datepicker fields, for example to let the user select his date of birth. The entered value is validated in the controller after posting the form, to make sure the age is over 18. This is working fine, except for the fact the datepicker changes the invalid date after postback to the first available valid date.
我的MVC3网站使用jQuery datepicker字段,例如让用户选择他的出生日期。在发布表单后,在控制器中验证输入的值,以确保年龄超过18.这是正常的,除了datepicker在回发后将无效日期更改为第一个可用的有效日期之外。
Submitting the form today with for example 11-11-2000 returns with a model error 'You need to be at least 18 years old'. The model is bound to the fields again, but instead of the entered date, the datepicker changes this to today, 18 years ago, which is 04-05-1997.
今天提交表单,例如11-11-2000,返回模型错误“你需要至少18岁”。模型再次绑定到字段,但是日期选择器不是输入日期,而是将其更改为18年前的今天,即04-05-1997。
How can I keep the original date the user entered in a not too hacky way?
如何以不太苛刻的方式保留用户输入的原始日期?
1 个解决方案
#1
Here you go!!
干得好!!
Js
$(document).ready(function(){
var today=new Date();
var year=today.getFullYear()-18;
var month=today.getMonth()+1;
var day=today.getDate();
var newDate=new Date(month +'/' +day + '/' +year);
$('#txtFromDate').datepicker({
maxDate: newDate
});
});
HTML
DOB: <input type="text" id="txtFromDate" readonly />
#1
Here you go!!
干得好!!
Js
$(document).ready(function(){
var today=new Date();
var year=today.getFullYear()-18;
var month=today.getMonth()+1;
var day=today.getDate();
var newDate=new Date(month +'/' +day + '/' +year);
$('#txtFromDate').datepicker({
maxDate: newDate
});
});
HTML
DOB: <input type="text" id="txtFromDate" readonly />