Solved

Custom RegularExpression (ASP.NET MVC4)

Posted on 2013-05-30
4
1,012 Views
Last Modified: 2013-06-18
In my MVC4 Model I need to make a custom RegularExpression that check if birth date has the correct format. The only thing is that birth date is formatted different in different countries.

In my case I need to accept Swedish format if country code = 46 and Norwegian format if contry code = 47. How can this be easily done?

The attached code shows what I try to do

namespace API.Models
{
    public class UserData
    {
        [Required]
        [RegularExpression([Custom RegEx here], ErrorMessage = "Invalid birth date")]
        public string BirthDate { get; set; }
    }

    public class CustomRegEx
    {
        public static string BirthDate()
        {
            if (CurrentSite.CountryCode == 46)
            {
                // Sweeden, format: yyyy.mm.dd
                return @"(19|20)\d\d[.](0[1-9]|1[012])[.](0[1-9]|[12][0-9]|3[01])";
            }
            else
            {
                // Norway, format: dd.mm.yyyy
                return @"(0[1-9]|[12][0-9]|3[01])[.](0[1-9]|1[012])[.](19|20)\d\d";
            }
        }
    }

}

Open in new window

0
Comment
Question by:webressurs
  • 2
4 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39207603
you don't need to use regex but CultureInfo of the country being requested.
here's the list of country codes:
http://codes.codedigest.com/CodeDigest/207-Get-All-Language-Country-Code-List-for-all-Culture-in-C---ASP-Net.aspx

for example, if the country is france:
var culture = New CultureInfo("fr-FR", True);
var datetime = "16/02/1992 12:15:12";
DateTime dt;
bool isValid = DateTime.TryParse(datetime ,culture, DateTimeStyles.None, out dt);

Open in new window

0
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 39207605
0
 
LVL 1

Author Comment

by:webressurs
ID: 39219116
Hi! I still can't get this correct. My ASP.NET Web API is used by sites with different culture info's. Based on the site that request the API I need to have different Date validation in the Model Class.

Is there any way to solve this in the Model Class?

Example:

namespace API.Models
{
    public class User
    {
        [Required]
        [IF CurrentSite.CountryCode == 46 use format yyyy.mm.dd]
        [Else If CurrentSite.CountryCode == 47 use format dd.mm.yyyy]
        [Else use default date format]

        public new string BirthDate { get; set; }
    }
}
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39220151
You can expand the setter of this property to apply the cultureinfo logic when u return the value.
public new string BirthDate { get{return value; }
set{
switch(CurrentSite.CountryCode){
case 46:
break;
case 47:
break;
default:
break;
}
 }
    }
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now