.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Code Snippets >> Javascript >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Date of Birth Should not be Less than 18 year validation using javascript

Posted By:Sanjeeb Lenka       Posted Date: November 30, 2013    Points: 40    Category: Javascript    URL: http://www.dotnetspark.com  

Date of Birth Should not be Less than 18 year validation using javascript

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript">
        function Validate() {
            var enteredValue = document.getElementById('<%=TextBox1.ClientID %>');
            var enteredAge = getAge(enteredValue.value);
            if (enteredAge < 18) {
                alert("DOB less than 18");
                return false;
        function getAge(DOB) {
            var today = new Date();
            var birthDate = new Date(DOB);
            var age = today.getFullYear() - birthDate.getFullYear();
            var m = today.getMonth() - birthDate.getMonth();
            if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
            return age;
    <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>: enter in DD/MM/YYYY<br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick=" Validate();" />


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend