[英]How to check both invalid date format and age greater than 18 years validation in one code?
I have written javascript like below 我写了下面的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>
In html 在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>
I want that the above javascript should check both validation. 我希望上面的javascript应该检查两个验证。 one is that the age should be above 18 years and the second one is if user enters date in wrong format I mean other than mm/dd/yyyy or m/d/yyyy format then the div with class datefomaterror should be visible?
一种是年龄应该在18岁以上,第二种是如果用户以错误的格式输入日期,我的意思是除mm / dd / yyyy或m / d / yyyy格式以外,则类datefomaterror的div应该可见?
Please help me!!! 请帮我!!! Also guide me in improving the javascript code.
还要指导我改善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);
One way of confirming the validity of a date is using the valueOf()
method in Date
. 确认日期有效性的一种方法是使用
Date
的valueOf()
方法。
example: 例:
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
You can use this to check if the date entered is valid of not. 您可以使用它来检查输入的日期是否有效。
isNan
checks if a number is Not a Number
. isNan
检查数字是否Not a Number
。
So your ValidateDate()
method can be something like this: 因此,您的
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;
}
}
Please note in this check -
and /
both give valid dates. 请注意此支票
-
和/
均提供有效日期。
Also here 也在这里
var currentTime = new Date();
currentTime = currentTime.format('dd-MMM-yyyy');
Date
does not have a format()
method, unless you have changed its prototype
. 除非您更改了
prototype
,否则Date
没有format()
方法。
You can try Moment.js to fiddle with dates - http://momentjs.com/ 你可以尝试用Moment.js日期捣鼓- http://momentjs.com/
Also check this out: Moment.js - how do I get the number of years since a date, not rounded up? 还请检查以下内容: Moment.js-如何获取自日期起的年数,而不是四舍五入?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.