Click here to Skip to main content
15,905,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Sir!

I have a textbox in my project name 'tbdob' & i also set a calendar in this textbox(tbdob).
Through this calendar i can only select the date (DOB).
now i want to set the validation:::
if the member age (calculate from the selected date in tbdob)is less than 18 or more than 50 then he is not allow for a member.

Please help me how i do this???

[EDIT - OP's code]
For set calendar i have used the code:
XML
<script type="text/javascript">

      $(document).ready(function () {
          $('#<%=tbmemberdob.ClientID%>').datepicker({
              dateFormat: 'mm-dd-yy',
              changeMonth: true,
              changeYear: true,
              //yearRange: '-50:+0',
              yearRange: '-50y:c+nn',
              maxDate: '-1d'
          });


      });

  </script>


For calculate the age i hv use the coode::
XML
<script type="text/javascript">
    function CalAge() {

    var now = new Date();
    var mm = document.getElementById('ddlmnths').value;
    var ddr = document.getElementById('ddldays').value;
    var dd = ddr.options[ddr.selectedIndex].value;
    var yy = document.getElementById('ddlyrs').value;
    bDay = dd + "/" + mm + "/" + yy;
    bD = bDay.split('/');
        if (bD.length == 3) {
               born = new Date(bD[2], bD[1] * 1 - 1, bD[0]);
                 years = Math.floor((now.getTime() - born.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
        if (years < 18) {
            alert("You are not eligible");

        }
        else if (years = 18 || years > 50) {
            alert("Welcome");
        }
    }
}
</script>
Posted
Updated 23-Apr-13 3:33am
v2
Comments
joshrduncan2012 23-Apr-13 9:16am    
I would suggest reading how the DateTime comparisons work to calculate ages. Do you want someone to do the work for you? What have you done so far (in terms of coding) to accomplish this problem?
prodipjsr 23-Apr-13 9:28am    
For set calendar i have used the code:
<script type="text/javascript">

$(document).ready(function () {
$('#<%=tbmemberdob.ClientID%>').datepicker({
dateFormat: 'mm-dd-yy',
changeMonth: true,
changeYear: true,
//yearRange: '-50:+0',
yearRange: '-50y:c+nn',
maxDate: '-1d'
});


});

</script>


For calculate the age i hv use the coode::
<script type="text/javascript">
function CalAge() {

var now = new Date();
var mm = document.getElementById('ddlmnths').value;
var ddr = document.getElementById('ddldays').value;
var dd = ddr.options[ddr.selectedIndex].value;
var yy = document.getElementById('ddlyrs').value;
bDay = dd + "/" + mm + "/" + yy;
bD = bDay.split('/');
if (bD.length == 3) {
born = new Date(bD[2], bD[1] * 1 - 1, bD[0]);
years = Math.floor((now.getTime() - born.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
if (years < 18) {
alert(“You are not eligible”);

}
else if (years = 18 || years > 50) {
alert(“Welcome”);
}
}
}
</script>

Try this code
XML
<script type="text/javascript">
    function AgeLimitCheck(sender,args)
{
var date1=new Date();
var date2 =sender._selectedDate;
var yearDiff=date1.getFullYear()-date2.getFullYear();
 if (yearDiff < 18 || yearDiff > 50 )
{
alert("You age must be in 18 to 50");
sender._selectedDate = new Date();

sender._textbox.set_Value(sender._selectedDate.format(sender._format))
}
}
    </script>


XML
<asp:textbox id="TextBox1" runat="server" ></asp:textbox>
        <cc1:calendarextender id="CalendarExtender1" runat="server" OnClientDateSelectionChanged="AgeLimitCheck" TargetControlID="TextBox1" >
    </cc1:calendarextender>
 
Share this answer
 
v2
Comments
prodipjsr 24-Apr-13 2:16am    
Sir, Show the error mess:
calendar does not exist in the namespace.
Pallavi Waikar 24-Apr-13 3:22am    
calendarextender is Ajax control...it mean's u have not added reference of AjaxControlToolkit
prodipjsr 24-Apr-13 4:05am    
I hve added this control:;
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
Pallavi Waikar 24-Apr-13 4:12am    
ok..it's part of understanding if ur TagPrefix is cc1 then u have to use cc1 at place of asp for ajax control...try following
<asp:textbox id="TextBox1" runat="server" >
<cc1:calendarextender id="CalendarExtender1" runat="server" OnClientDateSelectionChanged="AgeLimitCheck" TargetControlID="TextBox1" >


i have also update solution
prodipjsr 24-Apr-13 4:13am    
Sir, please help me another part of my project??
i have a textbox which property is readonly. in this textbox i am showing current date & time..when i run this & it is also open through IE. But when i click on the textbox & press Backspace button in keyboard,the age is loading & go to another page(open another page). i cant understand why it is going to another page...
Client side you can set min and max date values allowed.
JavaScript
$( "#datepicker" ).datepicker({ minDate: "-50Y", maxDate: "-18Y" });

read documentation: http://jqueryui.com/datepicker/#min-max[^]

And you should validate serverside as well.

C#
if(dob.AddYears(50) > DateTime.Now) // too old
if(dob.AddYears(18) < DateTime.Now) // too young
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900