ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Validate Date of Birth must be greater that 18 in ASP.Net<p>Refer below sample code for your reference and implement it as per your code logic.</p> <p><strong>HTML</strong> </p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head id=&#34;Head1&#34; runat=&#34;server&#34;&gt; &lt;title&gt;&lt;/title&gt; &lt;script src=&#34;http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.0.min.js&#34; type=&#34;text/javascript&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js&#34;&gt;&lt;/script&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css&#34; /&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/css/bootstrap-datepicker.css&#34; /&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/js/bootstrap-datepicker.min.js&#34;&gt;&lt;/script&gt; &lt;style type=&#34;text/css&#34;&gt; .table-condensed { width: 200px; height: 10px; font-size: 12px; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;br /&gt; &lt;br /&gt; &lt;div&gt; &lt;div class=&#34;row&#34;&gt; &lt;div class=&#34;col-md-4&#34;&gt; &lt;div id=&#39;datepicker&#39; class=&#34;input-group date datepicker col-md-6&#34; data-date=&#34;&#34; data-link-field=&#34;dtp_input2&#34; style=&#34;max-width: 220px&#34;&gt; &lt;asp:TextBox ID=&#34;txtDobDate&#34; runat=&#34;server&#34; CssClass=&#34;form-control&#34; size=&#34;16&#34; type=&#34;text&#34; value=&#34;&#34; AutoPostBack=&#34;true&#34; /&gt; &lt;span class=&#34;input-group-addon&#34;&gt;&lt;span class=&#34;glyphicon glyphicon-calendar&#34;&gt;&lt;/span&gt; &lt;/span&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class=&#34;row&#34;&gt; &lt;div class=&#34;col-md-4&#34;&gt; &lt;asp:CustomValidator ID=&#34;CvAgeValidation&#34; ErrorMessage=&#34;Age must be Greater than equal to 18&#34; ForeColor=&#34;Red&#34; ClientValidationFunction=&#34;AgeValidation&#34; runat=&#34;server&#34; /&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class=&#34;row&#34;&gt; &lt;div class=&#34;col-md-4&#34;&gt; &lt;asp:Button ID=&#34;btnSubmit&#34; Text=&#34;Submit&#34; runat=&#34;server&#34; CssClass=&#34;btn&#34;/&gt;&lt;br /&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { SetDatePicker(); $(&#39;#txtDobDate&#39;).change(function () { var age = CalculateAge(); if (age &lt; 18) { alert(&#34;Age must be Greater than equal to 18&#34;); } }); }); function CalculateAge() { var dob = $(&#34;#txtDobDate&#34;).val(); dob = new Date(dob); var today = new Date(); var age = Math.floor((today - dob) / (365.25 * 24 * 60 * 60 * 1000)); return age; } function AgeValidation(sender, args) { var age = CalculateAge(); if (age &gt;= 18) { args.IsValid = true; } else { args.IsValid = false; } } function SetDatePicker() { $(&#34;#datepicker&#34;).datepicker(); if ($(&#34;#txtDobDate&#34;).val() == &#34;&#34;) { var dateNow = new Date(); $(&#39;#datepicker&#39;).datepicker(&#34;setDate&#34;, dateNow); } } &lt;/script&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><strong>Screenshot</strong></p> <p><strong><img src="https://i.imgur.com/DLVpgm6.gif" alt="" width="383" height="224" /></strong></p>https://www.aspforums.net:443/Threads/459260/Validate-Date-of-Birth-must-be-greater-that-18-in-ASPNet/https://www.aspforums.net:443/Threads/459260/Validate-Date-of-Birth-must-be-greater-that-18-in-ASPNet/Mon, 04 Jun 2018 07:33:14 GMTValidate Date of Birth must be greater that 18 in ASP.Net<p>Check this function. It checks Months and even Days to verify Age.</p> <pre class="brush: html">&lt;asp:TextBox ID=&#34;txtDate&#34; runat=&#34;server&#34; Text=&#34;&#34; /&gt; &lt;asp:CustomValidator ID=&#34;CustomValidator1&#34; runat=&#34;server&#34; ClientValidationFunction=&#34;ValidateDOB&#34; ControlToValidate=&#34;txtDate&#34; ErrorMessage=&#34;&#34; ForeColor=&#34;Red&#34; /&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:Button ID=&#34;btnValidate&#34; Text=&#34;Validate&#34; runat=&#34;server&#34; /&gt; &lt;script type=&#34;text/javascript&#34;&gt; function ValidateDOB(sender, args) { //Get the date from the TextBox. var dateString = document.getElementById(sender.controltovalidate).value; var regex = /(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$/; //Check whether valid dd/MM/yyyy Date Format. if (regex.test(dateString)) { var parts = dateString.split(&#34;/&#34;); var dtDOB = new Date(parts[1] + &#34;/&#34; + parts[0] + &#34;/&#34; + parts[2]); var dtCurrent = new Date(); sender.innerHTML = &#34;Eligibility 18 years ONLY.&#34; if (dtCurrent.getFullYear() - dtDOB.getFullYear() &lt; 18) { args.IsValid = false; return; } if (dtCurrent.getFullYear() - dtDOB.getFullYear() == 18) { //CD: 11/06/2018 and DB: 15/07/2000. Will turned 18 on 15/07/2018. if (dtCurrent.getMonth() &lt; dtDOB.getMonth()) { args.IsValid = false; return; } if (dtCurrent.getMonth() == dtDOB.getMonth()) { //CD: 11/06/2018 and DB: 15/06/2000. Will turned 18 on 15/06/2018. if (dtCurrent.getDate() &lt; dtDOB.getDate()) { args.IsValid = false; return; } } } args.IsValid = true; } else { sender.innerHTML = &#34;Enter date in dd/MM/yyyy format ONLY.&#34; args.IsValid = false; } } &lt;/script&gt;</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://www.aspsnippets.com/Handlers/DownloadFile.ashx?File=37c1268d-fc06-429a-bcab-920b73fac0a1.gif" alt="" width="400" height="201" /></p>https://www.aspforums.net:443/Threads/459260/Validate-Date-of-Birth-must-be-greater-that-18-in-ASPNet/https://www.aspforums.net:443/Threads/459260/Validate-Date-of-Birth-must-be-greater-that-18-in-ASPNet/Mon, 11 Jun 2018 07:18:53 GMT