Reputation: 1702
I have written javascript like below
<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
<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. 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?
Please help me!!! Also guide me in improving the javascript code.
Upvotes: 0
Views: 3569
Reputation: 909
One way of confirming the validity of a date is using the valueOf()
method in Date
.
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
.
So your ValidateDate()
method can be something like this:
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
.
Use this fiddle here for more reference.
Upvotes: 1
Reputation: 9
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);
Upvotes: 1
Reputation: 3313
You can try Moment.js to fiddle with dates - http://momentjs.com/
Also check this out: Moment.js - how do I get the number of years since a date, not rounded up?
Upvotes: 0