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.