Solved

Custom RegularExpression (ASP.NET MVC4)

Posted on 2013-05-30
4
996 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

20 Experts available now in Live!

Get 1:1 Help Now