Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8244919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:01:05+00:00 2026-06-07T22:01:05+00:00

I am attempting to validate a date text box to make sure the correct

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:01:07+00:00Added an answer on June 7, 2026 at 10:01 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to have Javascript validate a user-entered zip code. If it is within
I am attempting to validate a query parameter for a date. If an invalid
So the situation is this: attempting to add a dropdown box using the jquery.multiselect
Based on the answer provided here , I am attempting to validate whether or
Attempting to set up an automated texting service for customers, where people can text
Attempting to use the data series from this example no longer passes the JSONLint
Attempting to make a NSObject called 'Person' that will hold the login details for
I am attempting to validate my object that encapsulates a list of other objects,
I am attempting to validate that the input into a textbox on a C#
I'm attempting to use 'jQuery Validate' on a form that requires an email address

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.