Introduction:
Here I will explain how to calculate age of person from date of birth and regular expression for mm/dd/yyyy format using JavaScript.
In some situation we need to display age of person based on date of birth .In that situation we can calculate age of person using JavaScript by comparing present date and date of birth as shown below. Here I have a one textbox every time I need to validate that data whether user enter valid data or not if the date not in mm/dd/yyyy format or not.
Here I will explain how to calculate age of person from date of birth and regular expression for mm/dd/yyyy format using JavaScript.
Description:
In some situation we need to display age of person based on date of birth .In that situation we can calculate age of person using JavaScript by comparing present date and date of birth as shown below. Here I have a one textbox every time I need to validate that data whether user enter valid data or not if the date not in mm/dd/yyyy format or not.
Regular expression for mm/dd/yyyy date format is
re=/^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$/;
|
If you want to implement code
for calculate age of person using javascript design your aspx page like this
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Calculate Age</title>
<script type="text/javascript">
function CalculateAge(birthday) {
var re=/^(0[1-9]|1[012])[-
/.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$/;
if (birthday.value != '') {
if(re.test(birthday.value ))
{
birthdayDate = new Date(birthday.value);
dateNow = new Date();
var years = dateNow.getFullYear()
- birthdayDate.getFullYear();
var months=dateNow.getMonth()-birthdayDate.getMonth();
var days=dateNow.getDate()-birthdayDate.getDate();
if (isNaN(years)) {
document.getElementById('lblAge').innerHTML
= '';
document.getElementById('lblError').innerHTML
= 'Input date is incorrect!';
return false;
}
else {
document.getElementById('lblError').innerHTML
= '';
if(months < 0 || (months ==
0 && days < 0)) {
years = parseInt(years) -1;
document.getElementById('lblAge').innerHTML
= years +' Years '
}
else {
document.getElementById('lblAge').innerHTML
= years +' Years '
}
}
}
else
{
document.getElementById('lblError').innerHTML
= 'Date must be mm/dd/yyyy format';
return false;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Date of Birth :<asp:TextBox ID="txtAge" runat="server" onblur="CalculateAge(this)" />(mm/dd/yyyy)
<span style="color: Red">
<asp:Label ID="lblError" runat="server"></asp:Label></span>
<br />
Age : <span id="lblAge"></span>
</div>
</form>
</body>
</html>
|
Demo
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
9 comments :
how can we validate radio button using java script(for eg:male,female)
It Should not be work.. the Date formate in asp.net re= ...... wotking as error.... plz provid correct formate
@Suresh Jaggarapu..
it will work in asp.net. please check your code.
i need in forms without javascripts sir can u provide
Hi Suresh Thanks for the code
But this will fail for the date FEBRUARY 31st or "11/31/2012"
It will not consider the no. of days in the alternate months
Syntax Error in regular expression
Hi ,
I am trying to use this . I have a text box in which i need to validate for the correct date format. the format will be mm/dd/yyyy. but when i am entering invalid date it is not generating any error.
and Seconly Feb month and Leap year validations also not working.
Pls suggest me if you have any option.
It is not working sir
Note: Only a member of this blog may post a comment.