aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Regular expression to validate date in mm/dd/yyyy format in JavaScript

Jul 25, 2012

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.

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="">
<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 '
document.getElementById('lblError').innerHTML = 'Date must be mm/dd/yyyy format';
return false;
<form id="form1" runat="server">
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&nbsp;&nbsp;&nbsp; : <span id="lblAge"></span>

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


mahender kumar said...

how can we validate radio button using java script(for eg:male,female)

Anonymous said...

It Should not be work.. the Date formate in re= ...... wotking as error.... plz provid correct formate

Suresh Dasari said...

@Suresh Jaggarapu..
it will work in please check your code.

Unknown said...

i need in forms without javascripts sir can u provide

robzz said...

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

Unknown said...

Syntax Error in regular expression

Anonymous said...

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.

Anonymous said...

It is not working sir

Ano mepani said...
This comment has been removed by the author.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.