有人能告诉我如何在表格中对日期输入类型进行验证,以计算当前日期与提供日期之间的差异,以衡量差异是否大于18年。
注意:可以在提交按钮上调用JavaScript并在警告框中显示结果。
答案 0 :(得分:2)
您可以简单地减去它们以获得毫秒差异。
var age = Math.floor((new Date() - new Date(dateString)) / (1000*60*60*24*365.25))
答案 1 :(得分:0)
您可以使用valueAsDate来获取与提交的值相对应的Date,并使用" now"日期。
HTML
<form id="myForm">
<input type="date" name="dateInput" id="dateInput" value="2013-08-01" />
<input type="submit">
</form>
JS
$(function() {
$('#myForm').submit(function() {
var _submittedDate = document.getElementById('dateInput').valueAsDate;
var _now = new Date();
var _milliPerYear =1000*60*60*24*365.26;
var _dateDifference = (_now - _submittedDate);
if ((_dateDifference/_milliPerYear) > 18) {
alert("VALID");
} else {
alert("Invalid");
}
return false; //Avoid form submission for testing
});
});
答案 2 :(得分:0)
你可以使用这样的东西。但请注意,这取决于Javascript的日期字符串格式(Date构造函数或Date.parse():https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)。您应该添加一个额外的检查,以确保您具有Date对象将能够解析的有效日期字符串。
$this->_addButton('save_and_continue', array(
'label' => Mage::helper('customer')->__('Save and Continue Edit'),
'onclick' => 'saveAndContinueEdit(\''.$this->_getSaveAndContinueUrl().'\')',
'class' => 'save'
), 10);
答案 3 :(得分:-1)
你可能应该为表单的提交添加一个事件处理程序,并检查该处理程序中的日期。如何计算年龄如下:
function getAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
console.log('age: ' + getAge("2010/08/10"));