Validation

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2009
Posts: 24
Reputation: foxypj is an unknown quantity at this point 
Solved Threads: 0
foxypj foxypj is offline Offline
Newbie Poster

Validation

 
0
  #1
Mar 3rd, 2009
I am currently working on a project where i need check whether a date that is inputted is valid.

i know how to use this using repetitive if functions.

for example;

to check the month you do

if (month <1 || month >12)

Then to check the day which is my problem. Each month has a different amount of days in it. i can see how to do an if loop which then has an inner loop.

i.e

if (month == 1)
{
if (day <1 || day > 31)
Application.Exit
}

then i would go on and do that for all twelve months changing the if function where necessary. but is there a way to do this with arrays?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Validation

 
0
  #2
Mar 3rd, 2009
Have you considered that you are reinventing the wheel?
Perhaps there is a function of the datetime variety that will do all this for you..
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 68
Reputation: srikanthkadem is an unknown quantity at this point 
Solved Threads: 11
srikanthkadem srikanthkadem is offline Offline
Junior Poster in Training

Re: Validation

 
0
  #3
Mar 3rd, 2009
Try with DateTme.TryParse method .let me know it is working for u.
got solution ?, please mark the thread as Solved. Thanks & Regards,
srikanth kadem
srikanthkadem@rediffmail.com
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 24
Reputation: foxypj is an unknown quantity at this point 
Solved Threads: 0
foxypj foxypj is offline Offline
Newbie Poster

Re: Validation

 
0
  #4
Mar 3rd, 2009
Originally Posted by srikanthkadem View Post
Try with DateTme.TryParse method .let me know it is working for u.

ok that apears to be working. Thankyou

My final problem. How would i go about outputting the date that i have just validated. like this.

i put in 22/03/09 originally then the program works out whether the day needs a nd, st, rd or th at the end of it and then outputs like this;

22nd March, 2009
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Validation

 
0
  #5
Mar 3rd, 2009
I was trying to get you to look at the datetime functions for yourself to find the things like tryparse..

Perhaps you will now try and do a search on date time formats.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 68
Reputation: srikanthkadem is an unknown quantity at this point 
Solved Threads: 11
srikanthkadem srikanthkadem is offline Offline
Junior Poster in Training

Re: Validation

 
0
  #6
Mar 3rd, 2009
i put in 22/03/09 originally then the program works out whether the day needs a nd, st, rd or th at the end of it and then outputs like this;

i did not get this words..can u explain indetails so that i can help u...
got solution ?, please mark the thread as Solved. Thanks & Regards,
srikanth kadem
srikanthkadem@rediffmail.com
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 24
Reputation: foxypj is an unknown quantity at this point 
Solved Threads: 0
foxypj foxypj is offline Offline
Newbie Poster

Re: Validation

 
0
  #7
Mar 3rd, 2009
ok I have now got the date inputted by the user in this format 12/12/1990 and have used the tryparse method to check for validity,



I now need to output that date as 12th December, 1990


i.e

Date:
Console.WriteLine("please insert a date")
date = Convert.ToString(Console.ReadLine())

String[] dateStrings = {date};
DateTime dateValue;


foreach (string dateString in dateStrings)
{
if (DateTime.TryParse(dateString, out dateValue))
goto output;
else
Console.WriteLine("\r\n {0}", date);
Console.WriteLine("\r\nError)


goto Date
}

I now want to output the date so that is outputs in a form like this 12th december 1990 instead of outputting as 12/12/09
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Validation

 
0
  #8
Mar 3rd, 2009
Check out this:
  1. string dateString;
  2. DateTime dateValue;
  3.  
  4. Console.WriteLine("please insert a date");
  5. dateString =Console.ReadLine();
  6. if (DateTime.TryParse(dateString, out dateValue))
  7. {
  8. //it is a date
  9. }
  10. else
  11. {
  12. //error
  13. }

Flush goto's...
Last edited by ddanbe; Mar 3rd, 2009 at 6:51 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Validation

 
0
  #9
Mar 3rd, 2009
Originally Posted by ddanbe View Post
Check out this:
  1. string dateString;
  2. DateTime dateValue;
Why do you do this? This is not C. Predeclaration makes code less readable.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 68
Reputation: srikanthkadem is an unknown quantity at this point 
Solved Threads: 11
srikanthkadem srikanthkadem is offline Offline
Junior Poster in Training

Re: Validation

 
0
  #10
Mar 4th, 2009
Hi Foxipj,
  1. dateValue.ToString("dd MMMM,yyyy");
  2. use this line in if condition .where datevalue is datetime object in ur code.
and u can play with this Tostring method as well for different formats.
got solution ?, please mark the thread as Solved. Thanks & Regards,
srikanth kadem
srikanthkadem@rediffmail.com
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC