[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Custom RegularExpression (ASP.NET MVC4)

Posted on 2013-05-30
4
Medium Priority
?
1,030 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:Meir Rivkin
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:
Meir Rivkin 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

656 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