How to validate a Date from String using Xojo.Core.Date.FromText()

Hi,

I want to validate a date entered by the user.
Before I used ParseDate() but as this seems to rely on the systems locale settings and not the users (its a Web App), I want to use Xojo.Core.Date.FromText instead.

How can I find out if a conversion fails?

dim test as xojo.Core.Date = Xojo.core.Date.FromText(me.Text, Xojo.Core.Locale.Current)

While debugging I see a RunTimeException and tried to use a try catch block. But on build it seems not to throw an error at all.

Thanks a lot.

Fabian

Using a TextField for a date entry is extremely shaky. For instance, how can you know when a user enters 12/10/2016 it is December 10, 2016 as it would seem natural in the US, or October 12, 2016 because the user is European ? Especially on the web where you never know where the user is, that can be a real issue.

The best way to get a valid date is simply to use a series of popupmenus for month, day and year. A vast majority of sites do that.

The most difficult part is to have the proper number of days according to the month, but you can get that easily from the date object as soon as the year and month are set.

Using the ISO presentation Year, Month, Day can facilitate that.

There are also datepickers available.

Thanks for this Idea Michel.

This would help but makes the input much slower. Isn’t there a way to validate the text like ParseDate would do?

As the name implies, you have to parse the date to compare it to a valid date.

Assuming you have a text date correctly formed, such as the 10/12/2016 I took as example, a split will let you parse month, day and year, then you can use Xojo.Core.Date FromText as a creator for a date object http://developer.xojo.com/xojo-core-date$FromText

Since it takes locale, you can then compare date.totext with what was entered.

Getting the user locale from the web app is not very convenient either :
http://stackoverflow.com/questions/673905/best-way-to-determine-users-locale-within-browser#674570

I would tend to let the user select his own way of entering dates, such as MMDDYYYY, DDMMYYYY or YYYYMMDD. That is what I do in Check Writer where indeed, users can enter dates themselves in a textfield.

[quote=299400:@Michel Bujardet]The most difficult part is to have the proper number of days according to the month, but you can get that easily from the date object as soon as the year and month are set.

Using the ISO presentation Year, Month, Day can facilitate that.[/quote]
FWIW most websites don’t really care and just put 31 days in the Day menu. It saves calculations and allows the user to enter the day faster as they don’t have to wait for the number of days to be found and the menu updated. Also, if you do this in Xojo Web without creating a custom Javascript-only version, you have to wait for the server round trip slowing input further.

Most people aren’t that dumb. However it would be worth validating the date before using it once the server has received it from the user.

Yes, thats what I already did as you can see in my sample code above. In WebApps you can read the users locale using Xojo.Core.Locale.Current. So my code should parse dates correct and it does. But it also parses wrong entered dates.
For Germany a correct date would look like dd.mm.yyyy - but Xojo.Core.Date.FromText with german locale set would also parse dd/mm/yyyy or mm/dd/yyyy or even dd,yy/yyyy.

I mean its really nice that Xojo parses nearly everything correctly into a date. I am just wondering if I could somehow verify that it was a correctly entered date. My Idea is to create a sample date internally, convert it to text with users locale settings and then compare the output format to the date entered.

Data validation is an art as much as a science. There will always be edge cases, and people really strange. Maybe you can stress how important it is to enter valid dates to the user, so if he messes up, he cannot blame it on you.