Click here to Skip to main content
16,004,507 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everyone..i am doing one e-commerce project.In that i am having problem in one of the page where customer sign up with his personal details.In date of birth column it is taking the values for today and future date also.I want values to be taken if it is earlier to tat day otherwise error should be displayed.How to do this?Please help me in validating date of birth.
Thank You.
Posted

C#
DateTime dOB = ...
DateTime now = DateTime.Now;
if (dOB < now)
    {
    ...
    }
You might want to look at this as well, perticularly if you have a minimum age you can accept orders from: Working with Age: it's not the same as a TimeSpan![^]
 
Share this answer
 
Comments
Maciej Los 21-Aug-13 7:07am    
Answer as answer, but tip... excellent!
Member 10194425 21-Aug-13 7:22am    
what i have to write in that first line?
datetime DOB=...?
OriginalGriff 21-Aug-13 8:37am    
Depends on where you get your user info from. If you use a Calendar control then you can get the value directly. If you use a text box (and that is really silly) then you need to use DateTime.TryParse on the text entered, and report any problems back to your user.

I can't fill it in because I can't see your screen, access your HDD or read your mind...:laugh:
Member 10194425 21-Aug-13 7:24am    
what i have to write in that first line..?
datetime dob=..?
Member 10194425 21-Aug-13 7:23am    
what i have to write in that first line..?
datetime dob=..?
Hello

this is how I did mine.

C#
DateTime selectedDOB = Convert.ToDateTime(DatePicker.Text);
        string cDate = DateTime.Now.ToShortDateString();

        DateTime currentDate = Convert.ToDateTime(cDate);
        if (selectedDOB < currentDate)
        {
           //Error message here
           
            
        }
 
Share this answer
 
Comparison of both dates will help you solve your issue. .Net allows you to do that comparison easily without any kind of conversions. Look into the this link[^] to get details about the results returned by the comparison of dates.
 
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