1
votes

I am attempting to validate a date text box to make sure the correct date format was entered.

I converted this vb6 code:

If (IsDate(txtBirthDate)) And (IsNumeric(Right(txtBirthDate, 4))))

into this C# code -

int output = 1;
DateTime output2;

if ((! DateTime.TryParse(txtBirthDate.Text, out output2)) & (!int.TryParse((txtBirthDate.Text.Substring(txtBirthDate.Text.Length - 5)), out output))) 
{
    MessageBox.Show("error")
}

What I am attempting to do is make sure that the last 4 digits of the date text box are numeric (the year - i.e 1990 in 05/10/1990) and if it is not a number, then show error. Although I cannot verify everything is numeric due to the "/" in the date format.

The code does not show an error and builds. But when I debug the application I receive an error. The error states:

Index and length must refer to a location within the string.
Parameter name: length.

Any ideas on how to accomplish this?

4

4 Answers

3
votes

To check if a date is in a specific format, use DateTime.TryParseExact():

if (!DateTime.TryParseExact(output , "d/M/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out output2)) 
{
    MessageBox.Show("error")
}

EDIT: Change the format according to your needs:

"d/M/yyyy" for UK and "M/d/yyyy" for US

2
votes

Edit: It sounds like the cause of your error is your string is too short. Test the string length before you test the last 4 characters.

Three other issues:

  1. The And operator in C# is &&. You are using & which is a bitwise operator.
  2. To check the last four characters of the string, you should use .Length - 4, not 5.
  3. You are negating the return values in your C#, but not in your VB. To match the VB, omit the !. But, it looks like that's not what you are actually trying to do. It looks like you want to show an error message if either the string is not parsable as a date or the year portion is not 4 digits. In that case use a an or comparison (||):
if (!DateTime.TryParse(txtBirthDate.Text, out output2) ||
    txtBirthDate.Text.Length < 4 ||
    !int.TryParse((txtBirthDate.Text.Substring(txtBirthDate.Text.Length - 4)), out output))
{
    MessageBox.Show("error")
}

Keep in mind that yyyy/M/d is also parsable as a Date, contains a 4-digit year, but will fail your test. Is that what you want?

0
votes

From the error provided it looks like you just need a length check to make sure you can process the code. You should also use && instead of & (or in this case probably ||) to ensure that the boolean expressions stop executing once a true state has been encountered.

if (txtBirthDate.Text.Length < 5 ||
   (!DateTime.TryParse(txtBirthDate.Text, out output2) ||   
    !int.TryParse((txtBirthDate.Text.Substring(txtBirthDate.Text.Length - 5)), out output))) 
{
    MessageBox.Show("error")
}

However, this might be a good case for the use of a regular expression.

Regex reg = new Regex("^\d{2}/\d{2}/\d{4}$");

if (!reg.IsMatch(txtBirthDate.Text))
    MessageBox.Show("error");

Tweaking of the regular expression to match fringe cases (leading zero's, alternate formats, etc) may be necessary.

0
votes
        int result;
        if (int.TryParse(txtBirthDate.Text.Substring(test.LastIndexOf("/")), out result))
        {
           //its good
        }
        else
        {
            //its bad
        }