Validate date

  • Thread starter Thread starter Philip396
  • Start date Start date
P

Philip396

It would also be helpful if someone could
help me find code that will validate the
date. Not the format but the date in other
words something like 2345/04/04.
 
You could try using Date.Parse or DateTime.Parse, but those will throw a
FormatException if the string is not a valid date, which might be a
performance issue for you. There is an IsDate function in the
Microsoft.VisualBasic namespace as well that does not throw an exception; it
returns a boolean value.


Brian Davis
http://www.knowdotnet.com
 
Well you could simply use the DateTime.Parse and a try/catch block to
validate the formatting is correct. Like this:

public bool CheckDate(string s)
{
bool valid = true;
try {DateTime dt = DateTime.Parse(s);}
catch (FormatException) {valid = false;}
return valid;
}

- Noah Coad -
Microsoft MVP
 
This works for all data type checks..

using System.Text.RegularExpressions //using statement to be used at the
module level.

Regex.IsMatch(Crtl.Text, "^[a-zA-Z0-9]+$") //Allows only numbers and
alphabets

The above statement returns a Boolean, with no exceptions thrown. Visit this
website for further help on some standard validation patterns, including
your Datetime Check..

http://www.regexlib.com/

Thanks
VJ
 
Back
Top