473,386 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to validate a date of birth (age) if minimum is 18 in C#

2
I have a textbox and a button and my code is like this:

Expand|Select|Wrap|Line Numbers
  1. DateOfBirth = DOB(TextBox1.Text);
  2.         try
  3.         {
  4.  
  5.             if (string.IsNullOrEmpty(TextBox1.Text))
  6.             {
  7.             }
  8.  
  9.             else
  10.             {
  11.  
  12.                 DateOfBirth = Strings.Trim(TextBox1.Text.ToString());
  13.                 DateTime DOB = DateOfBirth;
  14.                 int Years = 0;
  15.                 int Month = 0;
  16.                 int Days = 0;
  17.                 string StrAge = null;
  18.  
  19.                 if (DOB < DateTime.Now)
  20.                 {
  21.                     TimeSpan dateDiff = DateTime.Now - DOB;
  22.                     DateTime age = new DateTime(dateDiff.Ticks);
  23.                     Years = age.Year - 1;
  24.                     Month = age.Month - 1;
  25.                     Days = age.Day - 1;
  26.                     StrAge = ((Years.ToString() + " Years ") + Month.ToString() + " Months ") + Days.ToString() + " Days ";
  27.  
  28.                     ElseStrAge = "Entered Date Of Birth is less then Current Date";
  29.                 }
  30.                 LblStatus.Text = "Your Exact Age is : " + StrAge;
  31.             }
  32.         }
  33.         catch (Exception ex)
  34.         {
  35.             Response.Write(ex);
  36.         }
  37.     }
  38.  
  39.  
ERROR that Im getting:

The name 'DateOfBirth' does not exist in the current context


Where must i declare this cause I've tried many ways.
Feb 21 '11 #1
2 1352
Rabbit
12,516 Expert Mod 8TB
You haven't declared DateOfBirth. You just start using it out of no where.
Feb 21 '11 #2
Yeah.
I think rabbit is right.
Try declaring DateOfBirth globally.
It might help.
I might be wrong.


Regards
Ajinkya
Feb 22 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Frank Rocco | last post by:
Hello, What is the best way to validate a textbox to see if it contains a valid date or a valid time or both? Thanks Frank
2
by: Bogdan Zamfir | last post by:
Hi, I want to use RegularExpressionValidator to validate a date I want to let user to enter date in the following format one or two digits for day one or two digits for month two or four...
2
by: Grey | last post by:
I want to know that how to valiate date input from ASP.NET? can i use custom validator? if so, how to.. Million Thanks.
2
by: Fernando Lopes | last post by:
Hi. How can i validate a date textbox using validator, but the validator need to validate all data types, like: USA: 11/19/2004 BRAZIL: 19/11/2004 GERMANY: 19.11.2004 So, in the validator...
6
by: NH | last post by:
I want to allow users to enter dates in a text box in either the US "MM dd yy" format or the UK "dd MM yy" format. But how can I validate these dates? All the date functions e.g. ISdate,...
4
stormrider
by: stormrider | last post by:
Hi all, I'm trying to implement a Perl structure. My small program will do the followings; Take the birth date of the user as input. (Month's will be input as strings.) Take the current time...
2
by: Dinesh | last post by:
Hi experts, I am working on SQL server 2005 reporting services and i am getting a problem in writting a query. Situation is given below. There is one table in database Named Child Now i...
5
by: shapper | last post by:
Hello, What is the Regex expression to validate a date time format as follows: dd-mm-yyyy hh:mm:ss An example: 20-10-2008 10:32:45
1
vavc1980
by: vavc1980 | last post by:
Hello, I have a couple of textboxes where I enter a date range (From - To), I need to validate that the Date From is greater (future) or at least equal to the Date From, or else give an error. ...
12
by: lolodede | last post by:
i like to validate date of birth as format dd/mm/yyy i tried everything but is not working thank you
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.