Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am coding an validation for birthdate where the date must be 18 years old. I use for birthdate was a textbox with a date method.


I tried this code but the error was String was not recognized as a valid DateTime.

What I have tried:

txtBirthdate.Attributes["max"] = DateTime.Now.ToString("yyyy-MM-dd");

        DateTime BirthDate = DateTime.Parse(txtBirthdate.Text);
        DateTime CurrentDate = DateTime.Today;
        int Age = CurrentDate.Year - BirthDate.Year;
        if (Age < 18)
        {
            lbbday.Text = "Invalid Birth Day";
        }
Posted
Updated 7-Nov-17 16:33pm
Comments
san2debug 7-Nov-17 22:11pm    
can you confirm whether your getting error from first line or others
Graeme_Grant 7-Nov-17 22:43pm    
It sounds like an invalid [DateTime] character was entered. TryParse will fix that - see solution 2 below. However, you have another problem, see solution 3 below.

use DateTime.TryParseExact Method [^]

DateTime BirthDate;
          if (DateTime.TryParseExact("yyyy-MM-dd", txtBirthdate.Text.Trim(), System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out BirthDate))
          {
              DateTime CurrentDate = DateTime.Today;
              int Age = CurrentDate.Year - BirthDate.Year;
              if (Age < 18)
              {
                  lbbday.Text = "Invalid Birth Day";
              }
          }
          else {
              lbbday.Text = "Please enter a valid date (yyyy-MM-dd)";
          }
 
Share this answer
 
Comments
Graeme_Grant 7-Nov-17 22:36pm    
That is what I first thought but is not the problem ... Step-through debugging explains why... Have another closer look... then look at my solution. ;)
Edgar Bots 7-Nov-17 22:41pm    
I tried putting the codes and i tried putting the birthdate a today date but when i click add it accepted it. The validation won't work. i don't know how. please help thanks
Karthik_Mahalingam 7-Nov-17 22:48pm    
what is the value you are getting in txtBirthdate.Text.Trim() ?
Your code works as designed. Have you tried debugging[^]? Set a breakpoint on the first line and step through your code and watch the values:

txtBirthdate.Attributes["max"] = "2017-11-08"
BirthDate = {8/11/2017 12:00:00 AM}
CurrentDate = {8/11/2017 12:00:00 AM}
Age = 0

Now do you see, using debugging, what is actually happening in your code?
 
Share this answer
 
Hi Edgar Bots
You should remove "YYY-MM-DD" from ToString.It will work
Example
textBox1.Text = DateTime.Now.ToString();
            DateTime BirthDate = DateTime.Parse(textBox1.Text);
            DateTime CurrentDate = DateTime.Today;
            int Age = CurrentDate.Year - BirthDate.Year;
                if (Age < 18)
                {
                    textBox2.Text = "Invalid Birth Day";
                }

The code is working by removing that from tostring().
 
Share this answer
 
v2
Comments
Graeme_Grant 7-Nov-17 22:34pm    
This does not fix his problem. There was no problem in the first case with his code, only his understanding of what it was doing. Appears you had the same problem.

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