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 6839495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:43:32+00:00 2026-05-26T23:43:32+00:00

Im using DataAnnotations in an interface of a linq sql class. Thats all fine.

  • 0

Im using DataAnnotations in an interface of a linq sql class.
Thats all fine.

Im having problems with the date time fields

My code is as follows:

 [DataType(DataType.Date)]
    [RegularExpression(@"^([1-9]|0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$", ErrorMessage = "regexFail")]
    DateTime? DateofBirth { get; set; }

Now the datatype expression works fine, it brings in a date rather than a date time.
The problem lies in validation of the fields. My regex doesn’t match dates, even though i put it into an engine and it does. For instance i put “10/10/2010” in the field and i get the error “regexFail”.

I’m fairly sure my expression is good, so I’m not sure whats wrong.

Thanks in advance.

  • 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-05-26T23:43:32+00:00Added an answer on May 26, 2026 at 11:43 pm

    I think what is happening is that the DateTime value is being converted to a string, then matched against the pattern. If that’s the case, and ToString is being used, then a default time of 12:00:00 AM would be included in the string being matched.

    I tried the following code and IsValid returned true:

    string pattern = @"^([1-9]|0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d\s12:00:00\sAM$";
    
    RegularExpressionAttribute attribute = new RegularExpressionAttribute(pattern) { ErrorMessage = "regexFail" };
    DateTime dt = new DateTime(2010, 10, 10);
    
    bool isValid = attribute.IsValid(dt);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using DataAnnotations for validation of a custom class (LINQ to SQL auto generated)
I'm using linq to SQL and MVC2 with data annotations and I'm having some
I have the following code block: - using System.ComponentModel.DataAnnotations; public class NewsItem { [RegularExpression(System.Configuration.ConfigurationSettings.AppSettings[UrlRegEx],
I'm using DataAnnotations attributes to validate my model objects. My model class looks similar
I have a customer class which has both PhoneNumber and Email properties. Using DataAnnotations
I'm using DataAnnotations to validate my input fields on a MVC3 application. I'm using
Possible Duplicate: Entity Framework Code First: decimal precision I'm using Linq-to-Entities with the EntityFramework
Good day! I've the following ViewModel class I use for login form: using System.ComponentModel.DataAnnotations;
I'm writing an MVC2 app using DataAnnotations. I have a following Model: public class
I'm using dataannotations in an MVC2 app and am a little discouraged when trying

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.