Click here to Skip to main content
15,906,081 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends.
i have a text button in asp.net page , user will enter date in it. now i want that on the basis of date enter by user we will calculate his or her age ,
and if age is 18 or more then 18 years ,only in that situation it will allow user otherwiseit will show msg 'invalid date'
thanks in advance
Posted

You can either do the validation on your Button PostBack or create a CustomValidator to access the codebelow.

public bool IsLegalAge(string inputDate) 
{
            var bday = Convert.ToDateTime(inputDate);
            var ts = DateTime.Today - bday;
            var year = DateTime.MinValue.Add(ts).Year - 1;
            return year >= 18;
}


Good luck!
 
Share this answer
 
Comments
Pong D. Panda 12-Apr-11 4:09am    
Alright! My very first contribution to CP after 4 years of lurking!
Ankur\m/ 12-Apr-11 4:10am    
You can either do the validation on your Button PostBack or create a CustomValidator to access the codebelow.
onclientclick is the right event. Returning false will not post the page and an alert can be shown saying the age is invalid.
Also a server side check should be there to ensure if JavaScript is disabled.

Anyways my 5!
Different ways to do this discussed here.
 
Share this answer
 
try this

//In Page Load Or button click  whereever you want 

-------------------------------------------------------------
int k = CalculateAge(Convert.ToDateTime("10/25/1982"));

if (k>=18)
{
  //Valid
}

else
{
 //Invalid
}

-----------------------------------------------------------------  
  
//Function to return Date


 public static int CalculateAge(DateTime birthdate)
        {
            // get the difference in years
            int years = DateTime.Now.Year - birthdate.Year;
            // subtract another year if we're before the
            // birth day in the current year
            if (DateTime.Now.Month < birthdate.Month || (DateTime.Now.Month == birthdate.Month && DateTime.Now.Day < birthdate.Day))
                years--;
            return years;
        }


//Via Javascript

<script type="text/javascript">
        function age() {
            alert("Hii");
    var birthDay = document.getElementById("TextBox1");
 var now = new Date()
 var b_split = birthDay.value.split('/');
 if(b_split.length==3){
   var birthDate = new Date(b_split[2], b_split[1]*1-1, b_split

[0]);
   var years = Math.floor((now.getTime() - birthDate.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
   if (years >= 18) {
       
//Your Condition
   }
   else {
//Your Condition

      
   }
 }
}
</script>


 //On Button
 <asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="javascript:return age();"  />
 
Share this answer
 
v2
Comments
Ankur\m/ 12-Apr-11 4:03am    
The calculation should be done using JavaScript. A server side check will be there only to ensure the validation if JavaScript is disabled.

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