Windows Communication Foundation – 65 Links to make you an expert

March 20, 2008

You will find Sixty Five Videos and Virtual Labs to make you a WCF Expert  here.

Happy Programming!!!


Windows SharePoint Services 3.0 Tools: Visual Studio 2005 Extensions V 1.1

March 20, 2008

This is an add-on to Visual Studio that makes it easier to develop custom SharePoint applications (e.g. Web Parts, List Definitions, Site Definitions, stand-alone utility program etc) and which currently targets Visual Studio 2005.

You can download here.

Happy Programming!!!


Collection of Sharepoint Videos and Screencasts

March 20, 2008

Here is a  nice collection of videos from russ stalters, related to Sharepoint and WSS.

Hope it is helpful.

Happy Programming !!!


Collection of WCF (Indigo) links and material

July 16, 2007

I got educated on WCF (Windows Communication Foundation) from Microsoft. Training was apprehended in Microsoft Office. Training was given by Vineet Bhatia. Training was really good and best part was it contains bunch of labs rather then usual PPTs. Trainer provided collection of links, which lead us for step by step learning of WCF. Links covered topics from basic understanding to advance level concepts.

Day 1

¾ Step 1 – Read Principles of Service Design: Patterns and Anti-Patterns for an overview of SOA basics (~20 -30 min.)
http://msdn2.microsoft.com/en-us/library/ms954638.aspx

¾ Step 2 – Read Windows Communication Foundation Road Map to get an understanding of WCF goals, and its role in SOA enterprise architecture (~20 -30 min.)

http://msdn2.microsoft.com/en-us/library/aa480211.aspx

¾ Step 3 – Read On the road to Indigo: Prescriptive Guidance for Today’s Technologies by Richard Turner to get an understanding of current state of all Microsoft distributed application technologies (20-30 min)

http://blogs.msdn.com/richardt/archive/2004/03/05/84834.aspx

¾ Step 4 – Read Windows Communication Foundation Architecture Overview by Yasser Shohoud to learn WCF basics: Addresses, Bindings, and Contracts (~45 min.)

http://www.yassers.com/content/soa/WCFArchOverview.aspx

¾ Step 5 – Read Windows Communication Foundation Architecture Overview to learn WCF basics: Endpoints, Bindings, Contracts, Behaviors (~45 min.)
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnlong/html/wcfarch.asp)

¾ Step 6 – Complete virtual lab “Understanding Windows Communication Foundation” (90 min.)
http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?EventID=1032315324&EventCategory=3&culture=en-US&CountryCode=US

¾ Step 7 – Complete virtual lab “The Fundamentals of Programming the Windows Communication Foundation Virtual Lab” (90 min.)

http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?EventID=1032291422&EventCategory=3&culture=en-US&CountryCode=US

¾ Step 8 – Look through WCF latest news and announcements (~20 min.)

http://wcf.netfx3.com/blogs/news_and_announcements/default.aspx

Day 2

¾ Objectives for the day

  •  Understand how to define and modify Data Contracts
  •  Understand Server Instances and Service Hosting
  •  Understand Operations, Object Lifetimes, and Sessions
  •  Understand how to implement Asynchronous Messaging

Self-Study Steps

¾ Step 1 – Download and build the Data Contract Samples
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/WCF_Samples_MSDNLive/html/7a6107b5-eda1-491a-94a9-8265ba0c698b.asp

¾ Step 2 – Read the article “Serialization in Windows Communication Foundation”
http://msdn.microsoft.com/msdnmag/issues/06/08/ServiceStation/default.aspx

¾ Step3 – Read Craig McMurty’s Blog on “Versioning Windows Communication Foundation Services”
http://blogs.msdn.com/craigmcmurtry/archive/2006/07/23/676104.aspx

¾ Step 4 – Review the TechReady “Best Practices for Designing Service Bindings and Contracts” webcast (Craig McMurty, 75 minutes)
http://voyager/aspen/lang-en/management/LMS_TrainLedInfo.asp?UserMode=0&LEDefId=180636

¾ Step 5 – Read the article and review the code for “Discover Mighty Instance Management Techniques For Developing WCF Apps”
http://msdn.microsoft.com/msdnmag/issues/06/06/WCFEssentials/

http://msdn.microsoft.com/msdnmag/code/?url=http://msdn.microsoft.com/msdnmag/issues/06/06/WCFEssentials/default.aspx (Code)

¾ Step 6 – Read the MSDN Documentation on “Synchronous and Asynchronous Operations”
http://msdn2.microsoft.com/en-us/library/ms734701.aspx

¾ Step 7 – Read Steven M. Cohn’s blogs on “WCF: Asynchronous Operations”
http://weblogs.asp.net/stevencohn/archive/2007/02/05/asynchronous-operations.aspx

http://weblogs.asp.net/stevencohn/archive/2007/03/22/wcf-asynchronous-operations-ii.aspx

¾ Step 8 – Read the MSDN Article “Build a Queued WCF Response Service
http://msdn.microsoft.com/msdnmag/issues/07/02/Foundations/default.aspx

Day 3

¾ Objectives for the day:

  • Understand the options for hosting WCF enabled applications and the calling mechanisms supported.
  • Understand what a service contract is, how it works, and how to create one
  • Understand that contracts state minimum requirements that runtime configuration or the hosting environment may not support.
  • Learn how to build a queued WCF Service
  • Understand the specifics of the following, as applicable to WCF:
  • Sessions
  • Instancing
  • Concurrency

Self-Study Steps

¾ Step 1 – Read Designing and Implementing Services for a high level conceptual orientation to design and implement WCF services.
http://msdn2.microsoft.com/en-us/library/ms729746.aspx

¾ Step 2 – Read Hosting WCF Services
http://www.devx.com/codemag/Article/33655/1954?pf=true

¾ Step 3 – Read WCF Essentials – What You Need To Know About One-Way Calls, Callbacks, And Events by Juval Lowy (Source code included)
http://msdn2.microsoft.com/en-us/library/ms729746.aspx

¾ Step 4 – Read Build a Queued WCF Response Service by Juval Lowy (Source code included) for a brief introduction to WCF queued calls followed by an interesting problem-how to get results out of a queued call-and the solution via some cool WCF programming techniques.
http://msdn.microsoft.com/msdnmag/issues/07/02/Foundations/default.aspx

¾ Step 5 – Review Build a Queued WCF Response Service  on Dr Dobb’s Portal
http://www.ddj.com/dept/windows/196900749

¾ Step 6 – Read the MSDN article on Sessions, Instancing, and Concurrency
http://msdn2.microsoft.com/en-us/library/ms731193.aspx

Day 4

¾  Objectives for the day:

  • Become familiar with the extensibility mechanisms in WCF.
  • Understand WCF monitoring capabilities and acquire basic WCF debugging skills

Self-Study Steps

¾ Step 1 – Read Understanding Windows Communication Foundation Extensibility for an overview of WCF’s extensibility points (~20-30 min.)

http://weblogs.asp.net/andresv/archive/2006/10/19/Understanding-WCF-Extensibility.aspx

¾ Step 2 – Watch MSDN Architecture Webcast: Extending Windows Communication Foundation by Aaron Skonnard for a demonstration of several key extensibility points (~70 min.)
http://msevents.microsoft.com/cui/WebCastEventDetails.aspx?EventID=1032299314&EventCategory=5

¾ Step 3 – Watch the TechReady webcast “WCF Extensibility” by Craig McMurtry (~90 min.)

http://voyager/aspen/lang-en/management/LMS_TrainLedInfo.asp?UserMode=0&LEDefId=180620

¾ Step 4 – Review the following SDK extensibility samples (~180 min.)

¾ Step 5 – Review “Administration and Diagnostics” branch in MSDN documentation (~30 min.)
http://msdn2.microsoft.com/en-us/library/ms731055.aspx

Day 5

¾Objectives for the day:

  • Understand common interop, integration and migration scenarios
  • Learn basics of WCF reliable messaging and WCF transactions

Self-Study Steps

¾ Step 1 – Review the following integration and interop scenario guides (~120 min.)

¾ Step 2 – Review the following migration related articles (~120 min.)

¾ Step 3 – Read Windows Communication Foundation Transactions Overview to get an understanding of WCF transaction management functionality (~30 min.)

http://msdn2.microsoft.com/en-us/library/ms733904.aspx

Follow up by going through Writing a Transactional Application
http://msdn2.microsoft.com/en-us/library/ms229973(vs.80).aspx

¾ Step 4 – Go through Reliable Messaging demystified blog entry by Shy Cohen (~10 min.)

http://blogs.msdn.com/shycohen/archive/2006/02/20/535717.aspx

¾ Step 5 – Listen to Secure, Reliable Transacted Messaging with WCF (Part 1)  podcast on Channel 9 (~30 min.)
http://channel9.msdn.com/Showpost.aspx?postid=173405

¾ Step 6 – Listen to Secure, Reliable Transacted Messaging with WCF (Part 2)  podcast on Channel 9 (~30 min.)
http://channel9.msdn.com/Showpost.aspx?postid=173830

¾ Step 7 – View MSDN TV episode Reliable Messaging in Windows Communication Foundation (~15 min.)
http://msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20050825IndigoSC/manifest.xml

¾ Step 8 – Complete “Reliable and Transacted Messaging with the Windows Communication Foundation Virtual Lab” (~90 min.)

http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?EventID=1032291421&EventCategory=3&culture=en-US&CountryCode=US

Happy Programming!!!


.NET Framework 3.0 – Introduction and Useful Resources

July 10, 2007

Abstract
The .NET Framework 3.0 is the next generation of the .NET Framework that sits on the top of the previous version. It introduces some additional features, and in this article discussed these features in detail.

Article Contents:


Introduction

[ Back To Top ]

I have been come across many people thinking that WinFx is not related to .NET Framework.  The funniest answer I have gotten is that it is a fix related to Windows PC protection similar to WinFix.  It is good decision from Microsoft for changing its name from .NET Framework 3.0.  This article gives a clear explanation about the additional technologies/features that are included in .NET Framework 3.0, namely Windows Presentation Foundation (WPF), Windows Workflow Foundation (WF), Windows Communication Foundation (WCF) and Windows Card Space (WCS).

What Happens when we install Framework 3.0

[ Back To Top ]

Does it install new version of the Framework?  No. It is just an upgraded Framework from 2.0 that comes along with WPF (Avalon), WCF (Indigo), WCS (InfoCard) and WF. It is a Framework that sits on the top of the 2.0 Framework along with Common Language Runtime (CLR) and BCL (Base Class Library). Framework 3.0 comes with CLR version 2.0. We are still using version 2.0 compilers for the Framework 3.0. So if we have Framework 2.0 installed in our system, it will install managed API’s that are required for workflow, presentation, communication, etc. If Framework 2.0 is not installed, it will install Framework 2.0 and then install all other upgraded required components. The serious question that comes to mind is “why the version number is changed if we are still using 2.0 compliers.” The reason for choosing the new version number is Avalon, Indigo, Workflow, and Info card are all major new pieces of platform technology.

Features

[ Back To Top ]

· Managed Code Programming Model

· Includes WWF, WPF, WCF

· Delivers sophisticated User Experience

· New user interface code model with vector graphic support using WPF

· Advanced web services functionality using WCF

· Built in work flow for advanced business applications using WF

· Advanced security against phishing  using WCS

The below diagram (Figure 1) illustrates the architecture diagram of .NET Framework 3.0

Figure 1

 

Framework 3.0 is a layer above the .NET Framework 2.0 with the 4 major new components as mentioned earlier. The .NET application development takes place above the Framework 3.0. There is no up gradation to Visual Studio 2005, CLR 2.0, ADO.NET 2.0 and base class library. All these are part of .NET Framework 2.0. These technologies are developed as managed code API’s, therefore, all these technologies can be used in any .NET supported programming languages like C#, VB, J#, etc.

Windows Presentation Foundation (WPF)

[ Back To Top ]

This is formerly known as the code named “Avalon,” a graphical feature in Framework 3.0 that makes easy to build next generation web applications with the help of rich User Interface (UI), documents and media. This is used to display more advanced graphics that helps a developer to improve his/her designing skills using programming skills, which would be quite challenging. We developers can produce outstanding user interfaces using multimedia and document services in WPF. We can also make use of vector graphics, user interface, 2D and 3D drawing, fixed and adaptive documents, typography, raster graphics, animation, data binding, audio, video and develop graphic/animation through declarative programming. WPF allows developers as well as designers to collaborate and develop awesome visual user interfaces. Here are the two different developer environments that are used to make developer and designer work together.

1. Microsoft Visual Studio

2. Microsoft Expression Interactive Designer

The language that is used to develop application user interfaces in WPF is called XAML (Extensible Application Markup Language). XAML is based on XML (Extensible Markup Language). Separation of model and view is possible in XAML by placing design related information in FileName.xaml file and business logic is placed in FileName.xaml.cs file.

Core Components

The major components of WPF are:

1. Presentation Framework

2. Presentation Core

3. MILCore (Media Integration Layer)

4. DirectX

Presentation Framework and Presentation core are written in managed code. The DirectX engine is responsible for displaying. MILCore is written in unmanaged code in order to enable tight integration with DirectX. MILCore (MILCore.dll) also consists of a composition engine which is responsible for performance reasons.        

Microsoft Silverlight

WPF comes with its subset Microsoft Silverlight formerly named as Windows Presentation Foundation Everywhere (WPF/E) and is a subset of WPF which depends on XAML and JavaScript. Silverlight is a cross-browser, cross-platform plug-in for delivering the next generation of .NET based media experiences for the Web and mobile applications. Silverlight offers a flexible programming model that supports AJAX, VB, C#, Python, and Ruby, and integrates with existing Web applications. It is lightweight, just 1 MB download and pretty fast. We can play many videos simultaneously without stuttering or dropping frames. No doubt WPF is next-generation graphics API. More explanation on Silverlight is out of the scope of this article. For more details on Silverlight, visit http://www.microsoft.com/silverlight.

Windows Workflow Foundation (WF)

[ Back To Top ]

“Workflow” is a declarative way of implementing result oriented business process in software. WWF is a programming model that helps in defining, building, executing, debugging and managing work flow related applications that are in sync with business processes. It consists of a Microsoft NET Framework version 3.0 namespace, an in-process workflow engine, and designers for Visual Studio 2005.

We can build as many work flow styles as we need based on the requirement.

Graphical designer and debugger are provided to implement work flow related software. We can make use of imperative code along with declarative modeling.  It enables us to build workflow software that is more flexible and transparent.

Core Components

WF core components include:

1. Base Activity Library: This provides functionality for control flow, conditions, event handling, state management and invoking web service. One can build his or her own custom domain specific activities using the base activity.

2. Runtime Engine: This is responsible for Workflow execution and state management.

3. Runtime Services: This provides hosting flexibility and communication.

4. Visual Designer: It is responsible for graphical and code-based construction.

Once a workflow model is compiled, it can be executed inside any windows process including console applications, WinForms applications, Windows Services, ASP.NET Web sites, and Web services. Extensible Object Modeling Language [XOML] based on XAML is the language that is used for declaring the structure of workflow, business logic for the workflow.

In order to create workflow, activities using WWF are:

1. VS 2005 (comes by installing Visual Studio 2005 add-ins to design and program workflow)

2. SharePoint designer that permits building workflows for Share Point 2007

Windows Communication Foundation (WCF)

[ Back To Top ]

WCF is formerly known as the code “Indigo” is the first Unified Programming Model (UPM) for Service Oriented Applications (SOA). It is the unification of the technologies used to deliver distributed systems such as Enterprise Services, Messaging, .NET remoting, ASMX and WSE that run on the Microsoft platform. In other words, Windows Communication Foundation is an advanced technology to provide web services/remoting functionality with better features and reduces the time to develop a distributed system. It makes development interoperable with Non-MS Platform and integrates with existing products. We can build amazing services that would add more weight using WCF. WCF uses SOAP messages for communication between two processes. WCF has a set of API’s for creating systems that send messages between services and clients. The same API’s are used to create applications that communicate with other applications on the same system or on a system that resides in another company.

Core components

Here is a list of core components in WF.

1. End Point: A WCF service is exposed to the world as a collection of endpoints. It is the point where messages are sent or received. It consists of Address, Binding and Contract.

Address: End point consists of location where message can be sent/received. This is equivalent to a service address in WSDL. An example of Address components are URI, Identity & Headers.

Binding: This is a communication mechanism that describes how messages can be sent. This represents configuration. It is made up of various binding elements like Transport protocol, such as TCP, HTTP, MSMQ, named pipes, Encoding such as text, Message Transmission Optimization Mechanism such as MTOM, binary, and security like asymmetric, symmetric and transport.

Contract: It is a definition for a set of messages that can be sent or received (or both) at the address that describes what message can be sent. It describes the WCF contracts and their operations like One way, request/reply, duplex, and queuing.

2. Channel: A channel is a concrete implementation of a binding element. The channel is the implementation associated with that configuration.

3. Client: A program that exchanges messages with one or more endpoints using channels.

4. Service: A service is a construct that exposes one or more endpoints, with each endpoint exposing one or more service operations.

5. Behavior: A behavior is a component that controls various run-time aspects of a service, an endpoint, a particular operation, or a client.

· WCF has rich communication capabilities.

· WCF is 25%—50% faster than ASP.NET Web Services and approximately 25% faster than .NET Remoting.

· It is secured, Confidential in keeping messages.

· Using WCF message transfer is reliable.

Microsoft Windows Card Space (WCS)

[ Back To Top ]

It is formerly known as the code named “InfoCard” that helps to protect user’s digital identities against spoofing, phishing and tampering. It enables end users to provide digital identity to online services in a simple and trusted way.

Here is how it works…

Instead of authenticating users with passwords, websites authenticate users with security tokens. Submit identity token to the website with just a few clicks of a mouse. The website accepts this token presented by the user, decrypts the token, validates this credential and uses this information internally to identify the user. Cryptographic techniques along with responsible protocols are used for identification of the user. CardSpace includes a self-issued identity provider, which runs on the local Windows system and it can produce information cards just like any other identity provider.

Users download cards from identity providers such as their bank, employer, government agency, membership organization, or create their own self-issued cards. When a Website or Web service requests a user’s credentials, CardSpace will be invoked and allow the user to select a card to present. CardSpace then retrieves a verifiable credential from the selected identity provider, or the self-issuing authority as the case may be, utilizing interoperable protocols. It then forwards the credential to the target application. This provides users with a simple, secure and familiar sign-on experience that is consistent across all Websites and Web services.

We can enjoy the technology, simplicity, consistency and mainly security that Card Space gifts us.

Related Downloads

[ Back To Top ]

Microsoft .NET Framework 3.0 Redistributable Package

Description: The Microsoft .NET Framework version 3.0 redistributable packages install the common language runtime and associated files required to run applications developed to target the .NET Framework 3.0.

Microsoft Windows Software Development Kit for Windows Vista and .NET Framework 3.0 Runtime Components

Description: The Windows SDK includes content for application development with the API’s in Windows Vista, including the .NET Framework 3.0 technologies: .NET Framework 2.0, Windows Presentation Foundation, Windows Communication Foundation, Windows Workflow Foundation, and Windows Card Space. This SDK is designed for use with Windows Vista (which includes Framework 3.0). This release of the Windows SDK is compatible with Microsoft Visual Studio 2005 and the Visual Studio 2005 extensions for .NET Framework 3.0 (WCF & WPF), November 2006 CTP.

Visual Studio 2005 extensions for the .NET Framework 3.0, featuring plug-ins and templates to enable developers to use Visual Studio 2005 to build .Net Framework 3.0 applications

Visual Studio 2005 extensions for .NET Framework 3.0 (Windows Workflow Foundation)

Description: This version of Visual Studio 2005 extensions for .NET Framework 3.0 (Windows Workflow Foundation) requires the final released version of Windows Workflow Foundation Runtime Components, Microsoft Windows Vista, or the .NET Framework 3.0 Runtime Components.

4. Visual Studio 2005 extensions for .NET Framework 3.0 (WCF & WPF), November 2006 CTP

Description: The Visual Studio 2005 extensions for.NET Framework 3.0 (WCF & WPF), November 2006 CTP provides developers with support for building .NET Framework 3.0 applications using the released version of Visual Studio 2005.

5. Microsoft Silverlight

Microsoft Silverlight is a cross-browser, cross-platform plug-in for delivering the next generation of .NET based media experiences and rich interactive applications for the Web. Silverlight offers a flexible programming model that supports AJAX, VB, C#, Python, and Ruby, and integrates with existing Web applications. Silverlight supports fast, cost-effective delivery of high-quality video to all major browsers running on the Mac OS or Windows.

References

[ Back To Top ]

Microsoft .NET Framework 3.0 Community (NetFx3) Virtual Labs
Microsoft .NET Framework 3.0 Programming Model
Windows Presentation Foundation (WPF)
Windows Communication Foundation (WCF)
Introducing Windows CardSpace
Windows Workflow Foundation Overview
Files: ASP.NET Control for CardSpace

Conclusion

[ Back To Top ]

This article provided a complete overview on .NET Framework 3.0 and the features included in it. I will be discussing each of these technologies in depth in upcoming articles. So keep visiting ASPAlliance.com!

Note: Framework 3.0 does not include LINQ, DLINQ and all the new features that are included in C# 3.0. These features are going to be included in the next release ORCAS. NET FX 3.0 is supported on XPSP2, Win2k3, and Vista.

Upcoming Framework 3.5: As mentioned earlier it includes new language features, LINQ, DLINQ, 3.0 version of CLR, new classed for base class library and VS 2008 support for WF, WCF, WPF that includes new workflow-enabled services technology.

Reference:
ASP Alliance


Time Vaidation via Regular Expression

July 5, 2007

The function takes an input string. The format of the regular expression needs some explaining. Let’s talk about what is allowed. Hours must be present and can be one or two digits. That is \d{1,2} at the start. After that must be a colon. Then the minutes (2 digits) must be present. After that, everything starts to become optional. However, if seconds are present, then there must be a colon and two digits. So the colon and \d{2} is surrounded by parentheses and then followed by a question mark. The question mark says that the whole grouping in parentheses must appear exactly zero or one time. Then we do another grouping that must appear zero or one time. Inside this grouping is a space followed by some characters. The brackets are an indicator that “one of the letters” is correct. So after the space must be an upper or lower case “a” or “p”. After that, an upper or lower case “m” can appear (the question mark after the bracket indicates that the “m” must appear zero or one time).

So, that whole regular expression is used to validate the incoming string. Assuming the string is valid, then we can look to see if the individual pieces are in the right ranges. We split the value into an array based on the colon. So, if the incoming string is “12:25″ then we’ll have two values in our array: “12” and “25”. Note that if the incoming string is “12:25 PM”, we’ll still have two values: “12” and “25 PM”.

The first piece must be between 0 and 23. If there is an “a” or “p” in the value, then we set hasMeridian to true and the first piece must be between 1 and 12. The second piece must be between 0 and 59. The third piece, if present, must be between 0 and 59.

Note that we’re taking advantage of the way parseFloat works. If the string is “25 PM”, parseFloat will see the numbers at the start and then the space and stop evaluating – it will return a value of 25. So we don’t have to worry any more about the meridian part being in the value – it will not affect our evaluating of the range of numbers.

function isValidTime(value)
{
var hasMeridian = false;
var re = /^\d{1,2}[:]\d{2}([:]\d{2})?( [aApP][mM]?)?$/;

if (!re.test(value)) { return false; }

//check for time is in meridian or its 24hour?
if
(value.toLowerCase().indexOf(“p”) != -1) { hasMeridian = true; }
if (value.toLowerCase().indexOf(“a”) != -1) { hasMeridian = true; }

var values = value.split(“:”);

//Hours must be present and can be one or two digit.
//if time is not a meridian then value should less then 24 hour
//and greater then 0

if
( (parseFloat(values[0]) < 0) || (parseFloat(values[0]) > 23) ) { return false; }

//if time is in meridian then value should less then 12 hour and
//greater then 1 hour
if
(hasMeridian)
 {
   if ( (parseFloat(values[0]) < 1) || (parseFloat(values[0]) > 12) ) 
   { return false; }
 }

//Minutes must be present and can less then 60.
if ( (parseFloat(values[1]) < 0) || (parseFloat(values[1]) > 59) ) { return false; }

//if Second part is present then it should be less then 60.
if (values.length > 2)
{
  if ( (parseFloat(values[2]) < 0) || (parseFloat(values[2]) > 59) ) 
  { return false; }
}

 return true;
}

The function returns true if the time is valid and false if it is not. So, your validation could look something like:

if (!isValidTime(myTimeString)) { alert(“The time is not in the correct format.”); }

Reference:
Breaking Par Consulting Inc.

happy programming!!!


Date Validation via Regular Expression

June 29, 2007

The function takes an input string and an optional format string. The format string is only 3 characters long and determines the relative positions of the month, day, and year. If the format string is omitted, then it will be “MDY” (month first, then day, then year). There are three different types of dividers that can be used in the date string. These are the slash (/), the period (.) and the dash (-). Years can be either 2 digits (00-49 are assumed to be 21st century and 50-99 are assumed to be 20th century) or 4 digits.

The way the regular expression part of the function works is by using the “remember” capabilities of regular expressions. This assures that the same divider is used in both positions. (In other words, you couldn’t do MM/DD-YYYY). The “\1″ in the regular expression says “use the same thing you used in parentheses before (which is the check for a slash, period, or dash) and apply the check here”.

If the string passes the regular expression (there are 2 checks since the year can be either 2 or 4 digits), then additional JavaScript is performed to check the validity of the date. Instead of doing all the checks for the day of the month being out of range and checking for leap years, a simple approach is taken. A new date object is created in JavaScript. If the numbers are out of range (like February 31), JavaScript will still create an object, it will just be adjusted to be a valid date. So create the date and then check to see if it was adjusted by JavaScript. If it was adjusted, then the original date is not a valid date.

function isValidDate(dateStr, format)
{

//check if 2nd parameter contains valid value or not
//if not valid then set default format = “MDY”

if (format == null) { format = “MDY” }
format = format.toUpperCase();
if (format.length != 3) { format = “MDY” }
if ( (format.indexOf(“M”) == -1) ||
(format.indexOf(“D”) == -1) ||
(format.indexOf(“Y”) == -1)
)
{ format = “MDY” }

if (format.substring(0, 1) == “Y”)
{ // If the year is first
var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
}
else if (format.substring(1, 2) == “Y”)
{ // If the year is second
var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
}
else
{ // The year must be third
var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
}

// If it doesn’t conform to the right format
//(with either a 2 digit year or 4 digit year), fail

if ( (reg1.test(dateStr) == false) &&
(reg2.test(dateStr) == false)
)
{ return false; }

// Split into 3 parts based on what the divider was
var parts = dateStr.split(RegExp.$1);

// Check to see if the 3 parts end up making a valid date
//extract month part

if (format.substring(0, 1) == “M”) { var mm = parts[0]; }
else if (format.substring(1, 2) == “M”) { var mm = parts[1]; }
else { var mm = parts[2]; }

//extract day part
if (format.substring(0, 1) == “D”) { var dd = parts[0]; }
else if (format.substring(1, 2) == “D”) { var dd = parts[1]; }
else { var dd = parts[2]; }

//extract Year part
if (format.substring(0, 1) == “Y”) { var yy = parts[0]; }
else if (format.substring(1, 2) == “Y”) { var yy = parts[1]; }
else { var yy = parts[2]; }

//if year is in 2 digit
//00-49 are assumed to be 21st century and 50-99 are assumed to be 20th century
if (parseFloat(yy) <= 50)
{ yy = (parseFloat(yy) + 2000).toString(); }
if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }

var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);

if (parseFloat(dd) != dt.getDate()) { return false; }

if (parseFloat(mm)-1 != dt.getMonth()) { return false; }

return true;

}

The function returns true if the date is valid and false if it is not. So, your validation could look something like:

if (!isValidDate(myDateString, “DMY”)) { alert(“The date is not in the correct format.”); }

Reference:
Breaking Par Consulting Inc.

happy programming!!!


Follow

Get every new post delivered to your Inbox.