?
Solved

Custom RegularExpression (ASP.NET MVC4)

Posted on 2013-05-30
4
Medium Priority
?
1,027 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

765 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