Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
we take a textbox to enter date of birth.The date of birth in the textbox is compared with current date then calculate exact age.If the calculated age is less than 18,display error message.kindly give the code for this.
Posted
Updated 16-Jan-12 21:07pm
v2
Comments
Sergey Alexandrovich Kryukov 17-Jan-12 0:47am    
What have you done so far? The problem is very simple. Why text box?
--SA
Sergey Alexandrovich Kryukov 17-Jan-12 0:49am    
Please, type of the application and UI library. There is more then one type called TextBox.
Also, use DateTimePicker instead.
--SA
Sergey Alexandrovich Kryukov 17-Jan-12 0:58am    

Suggestion to all experts:

We should stop answering UI questions based on assumptions on the library to be used, as well as any other ambiguous questions. Some inquirers probably don't even suspect that there are at least three types named "TextBox". This is not an excuse. Answering based on assumption is not a good option, answering in more than one variant is not an option. We should wait until the inquirer eliminates the ambiguity.



In this case, I know the solutions which does not depend much on the UI library. I just don't want to make exclusions. The question mention a UI type which is specified with ambiguity. I just want to make this service more useful. It will be more useful if we insist on correct questions. In this way, we would devote more attention to the inquirers who deserve more attention.

Thank you for understanding.
--SA
Anuja Pawar Indore 17-Jan-12 3:08am    
Removed extra pre tag

1 solution

Sample code change as per your requirement

bool m_flag = false;
DateTime dt = new DateTime(1983, 5, 1, 8, 30, 52);
        DateTime dt1 = DateTime.Now;
        string strDate = dt.ToString("M/dd/yyyy");
        string strDate1 = dt1.ToString("M/dd/yyyy");
        if (compareDates(Convert.ToDateTime(strDate1), Convert.ToDateTime(strDate)))
        {
            m_flag = true;
            Response.Write("Valid");
        }
        else
        {
            Response.Write("Invalid");
        }

<pre>public bool compareDates(DateTime dtHireDate, DateTime dtDateOfBirth)
    {
        int age = dtHireDate.Year - dtDateOfBirth.Year;
        if (dtHireDate.Month < dtDateOfBirth.Month || (dtHireDate.Month == dtDateOfBirth.Month && dtHireDate.Day < dtDateOfBirth.Day))
            age--;
        if (age >= 18)
        {
            m_flag = true;
        }
        else
        {
            m_flag = false;
        }
        return m_flag;
    }

 
Share this answer
 

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