Click here to Skip to main content
15,892,697 members
Articles / Desktop Programming / WPF

WPF.JoshSmith

Rate me:
Please Sign up or sign in to vote.
4.99/5 (51 votes)
13 Jul 2008CPOL5 min read 390.9K   4.8K   263  
A free library of controls and utility classes for use in WPF applications.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;

namespace Test.RegexValidator
{
	public static class DateFormatRegex
	{
		/// <summary>
		/// Returns a regular expression which can be used to validate date strings,
		/// based on the current culture of the UI thread.
		/// </summary>
		public static string DateRegex
		{
			get
			{
				// M/d/yyyy (USA)
				// dd/MM/yyyy (Great Britian)			
				//System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo( "en-GB" );

				string format = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
				//Debug.WriteLine( "DateTime Format: " + format );

				string regex = null;

				switch( format )
				{
					case "M/d/yyyy":
						regex = @"^(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$";
						break;

					case "dd/MM/yyyy":
						regex = @"^(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)\d\d$";
						break;

					default:
						// I'm too lazy to figure out regexs for all the date formats.
						// If you need to account for some other culture, just add another
						// case to this switch with the regex in it.
						Debug.Fail( "Unexpected date format: " + format );
						break;
				}

				return regex;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Josh creates software, for iOS and Windows.

He works at Black Pixel as a Senior Developer.

Read his iOS Programming for .NET Developers[^] book to learn how to write iPhone and iPad apps by leveraging your existing .NET skills.

Use his Master WPF[^] app on your iPhone to sharpen your WPF skills on the go.

Check out his Advanced MVVM[^] book.

Visit his WPF blog[^] or stop by his iOS blog[^].

See his website Josh Smith Digital[^].

Comments and Discussions