如何在一个代码中检查无效的日期格式和超过18年的有效期
How to check both invalid date format and age greater than 18 years validation in one code?
我已经编写了如下的javascript
<script type="text/javascript">
function ValidateDate() {
var dt = document.getElementById("<%=txtDOB.ClientID%>").value;
var currentTime = new Date();
var inputdate = dt;
currentTime = currentTime.format('dd-MMM-yyyy');
if (dt == "" || dt == undefined) {
document.getElementById("<%=lblValidDate.ClientID%>").style.display = "none";
}
else {
var inputdate = dt;
if (new Date(inputdate).getYear() <= new Date(currentTime).getYear() - 18) {
document.getElementById("<%=lblValidDate.ClientID%>").style.display = "none";
return true;
}
else if(new Date(inputdate).getYear() >= new Date(currentTime).getYear() - 18)
{
document.getElementById("<%=lblValidDate.ClientID%>").style.display = "block";
return false;
}
else {
document.getElementById("<%=lblValidDate.ClientID%>").style.display = "none";
return false;
}
}
}
</script>
在html 中
<div style="display:none" id="dateformaterror">Please enter date in mm/dd/yyyy or m/d/yyyy format</div>
<asp:Label runat="server" ID="lblValidDate" Style="color: Red; display: none;"><b>Minimum Age should be 18 years old</b></asp:Label>
<asp:TextBox ID="txtDOB" CssClass="datepiker" runat="server" onchange="ValidateDate()"></asp:TextBox>
我希望上面的javascript应该检查这两个验证。一个是年龄应该在18岁以上,第二个是如果用户以错误的格式输入日期,我的意思是不是mm/dd/yyyy或m/d/yyyy格式,那么带有类datefomateror的div应该是可见的?
请帮帮我!!!同时指导我改进javascript代码。
var start ="1989-01-10";
// ex) 2014-08-10
if (start.length != 10 || start.indexOf("-") < 0) {
alert("[Error] Check date form");
return null;
}
// get Age
var nowDate = new Date();
var birth = new Date(start);
var now = nowDate.getFullYear();
var my =birth.getFullYear();
var myAge = now - my -1;
if(myAge < 18) {
alert("You are so young");
}
else if (myAge <0) {
alert("Please,Try again");
}
else
alert("You are "+ myAge);
Date
中使用valueOf()
方法。
示例:
var a = new Date('12/15/2015');
isNaN(a.valueOf()); // false, this means it is a valid date.
// whereas an invalid date would return true
a = new Date('14/15/2015');
isNaN(a.valueOf()); // true
您可以使用它来检查输入的日期是否有效。isNan
检查一个数字是否为Not a Number
。
所以你的ValidateDate()
方法可以是这样的:
function ValidateDate() {
var inputDateString = document.getElementById('date').value,
inputDate = new Date(inputDateString),
invalidErrorMessage = document.getElementById('invalid'), // Replace with your ID
below18ErrorMessage = document.getElementById('below18'); // Replace with your ID
invalidErrorMessage.style.display = 'none';
below18ErrorMessage.style.display = 'none';
if (!inputDateString.trim() || isNaN(inputDate.valueOf()) {
invalidErrorMessage.style.display = "block";
return;
}
if (new Date().getFullYear() - inputDate.getFullYear() < 18) {
below18ErrorMessage.style.display = "block";
return;
}
}
请注意,在这张支票中,-
和/
都给出了有效日期。
也在这里
var currentTime = new Date();
currentTime = currentTime.format('dd-MMM-yyyy');
Date
没有format()
方法,除非您更改了其prototype
。
在这里使用这个小提琴以获得更多参考。
您可以尝试Moment.js来篡改日期-http://momentjs.com/
还可以看看这个:Moment.js-我如何获得约会后的年数,而不是四舍五入?
相关文章:
- JavaScript 时刻格式日期字符串
- 时刻.js转换 MM/dd/yyyy 格式日期
- JQGrid - 日期过滤器不适用于日期格式(日期(1453636335000))
- Jquery UI 日期选择器设置日期不适用于格式日期的动态更改
- 解析网页上的自由格式日期条目
- Javascript - 查找两个 MM-dd-yyyy HH:mm:ss 格式日期之间的秒数
- Javascript格式日期
- angularJs指令格式日期
- 如何解析 yyyy-MM-dd HH:mm:ss.javascript中的SSS格式日期
- JS原生格式日期方法
- JavaScript格式日期从DDMMYYYY到DD/MM/YYYY
- 不能转换成正确的格式日期&来自fullcalendar事件对象的小时
- 从unix - time格式日期为yyyy-mm-dd
- 如何在引导日历中将英语格式日期更改为西班牙语
- 使用包含英国格式日期的字符串设置new Date()
- 解析ISO-8601格式日期在ie9中不工作
- 格式日期从dd/MM/yyy hh: MM:ss到yyyy/MM/dd hh: MM:ss
- 如何获得正确的格式日期在前一个月与谷歌应用程序脚本
- Moment js格式日期与时区
- 格式日期&在kendo.ui.GridColumn