Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date format validation

Status
Not open for further replies.

mtessier

Programmer
Jun 25, 2008
37
US
Hello All,

As per this article by Peter Blum, I am validating my date fields in dd/mm/yyyy format as follows:

BasePage.vb:
Code:
Protected Overrides Sub InitializeCulture()
     Dim setLang As String
     setLang = Language & "-ca"

     Dim newCulture As CultureInfo = CultureInfo.CreateSpecificCulture(setLang)
     newCulture.DateTimeFormat.LongDatePattern = "yyyy-MM-dd"
     newCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy"
     newCulture.DateTimeFormat.DateSeparator = "/"
     Thread.CurrentThread.CurrentCulture = newCulture
     Thread.CurrentThread.CurrentUICulture = newCulture

     MyBase.InitializeCulture()
End Sub

.aspx page:
Code:
<asp:CompareValidator ID="cpvDOBDateFormat" runat="server" ControlToValidate="mdbDoB" CultureInvariantValues="true" Display="None" EnableClientScript="false" ErrorMessage="<%$ Resources: Translation, NotDate %>" Operator="DataTypeCheck" Type="Date" ValidationGroup="MemberProfile" EnableViewState="false" />

My issue is that while validation passes for dd/mm/yyyy, it also passes for yyyy/mm/dd. In the background this "invalid" date is converted and saved in the database correctly, but our business users don't like the fact that they can enter a date in a format other than dd/mm/yyyy and the validation passes. I have played around with my InitializeCulture code (ie. removing the LongDatePattern, DateSeparator, MyBase call, etc.) but the screen still accepts the invalid date format. Is there anything I can do on the CultureInfo side, or will I have to manually validate the format of all dates? Keep in mind I'm using ASP.NET 2.0 without AJAX. Our site accomodates en-ca and fr-ca cultures, but the date input and display format is to remain as dd/mm/yyyy regardless of the language selection. The LongDatePattern is there because we transfer dates to and from the database in text format yyyy-mm-dd.

Any ideas?

Thanks,

Mike
 
I've never had any need to do anything with cultureInfo. If all users regardless of culture only want a format of "dd/mm/yyy" then why bother setting that object at all?

Also, could you use a calendar control instead of a free form textbox? That would take care of the user incorrectly formatting the date.
 
The CultureInfo override is required for the CompareValidator to do a DataTypeCheck on a Date field because it (supposedly) uses the ShortDatePattern property of CultureInfo. I have now proven that is is not 100% true. Based on the coding that I've demonstrated, the date validation fails for mm/dd/yyyy (correct) and passes for dd/mm/yyyy (correct) and yyyy/mm/dd (incorrect).

We're using an old-school style custom calendar control for users to select a date, but business users in departments where speed is paramount can type so quickly that it would break their momentum if they have to stop and click the calendar control. Therefore, we've allowed them to also be able to type into the text box. This is where the issue lies. Clicking the calendar control returns the date in dd/mm/yyyy format, but if they type in the wrong format the screen validation should fail.
 
Another option would be to use the Regex validator or write the regex yourself to allow only the formats you want. There are online sites that can help you build the regex.
 
That's a scenario I was hoping to avoid since there's several date fields in multiple pages that need to be changed, but at this point it looks like my only good option.

Thanks,

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top