Birth Date Validation in JavaScript

Html Code for datepicer
  1. <div class="form-group" style="margin-left:250px">  
  2.     <label class="col-sm-2 control-label">Birth Date:</label>  
  3.     <div class="col-sm-3">  
  4.         <p class="input-group">  
  5.             <input type="text" name="bday" id="bday" class="form-control" ng-focus="open($event)" ng-blur="out()" datepicker-popup="{{format}}" ng-model="newuser.bdate" is-open="opened" readonly="readonly" />  
  6.             <span class="input-group-btn">  
  7.                 <button type="button" class="btn btn-default" ng-click="open($event)">  
  8.                     <i class="glyphicon glyphicon-calendar"></i>  
  9.                 </button>  
  10.             </span>  
  11.         </p>  
  12.     </div>  
  13. </div>  
 
You are select a 18/05/2015
Than alert message display a error
 
 
If you entered up to 18 year then out put look like this
 
 
 
JavaScript Code for Date control
  1. //On focus out   
  2. $scope.out = function()   
  3. {  
  4.     var x = new Date($("#bday").val());  
  5.     var Cnow = new Date();  
  6.     if ($("#bday").val() == "")   
  7.     {  
  8.         $("#bday").focus();  
  9.     }   
  10.     else if (Cnow.getFullYear() - x.getFullYear() < 18)   
  11.     {  
  12.         alert('You Are Not 18 Year');  
  13.         $("#bday").val('');  
  14.     }  
  15.     else {}  
  16. };  
This code work focus out time if valid the no error found otherwise display error you are not 18 year old.
 
Thank You
Ebook Download
View all
Learn
View all