Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 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
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.

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&nbsp;&nbsp;&nbsp; : <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.

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

9 comments :

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 asp.net re= ...... wotking as error.... plz provid correct formate

Suresh Dasari said...

@Suresh Jaggarapu..
it will work in asp.net. 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 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.