Solved

Custom RegularExpression (ASP.NET MVC4)

Posted on 2013-05-30
4
1,002 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

16 Experts available now in Live!

Get 1:1 Help Now