Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Date validation (non existing dates...) #130

Closed
jdriesen opened this issue Sep 19, 2016 · 12 comments
Closed

Date validation (non existing dates...) #130

jdriesen opened this issue Sep 19, 2016 · 12 comments

Comments

@jdriesen
Copy link

Hi Sir,

Congraz with the development of this great validator.
Though, it seems there is still a small problem with the dates...

I'm using the date_euro_long validator

Giving 31/04/2001 (April 31st) seems to be accepted as a valid date (though April has max 30 days...)

Testing a bit further ...
30/02/1968 (Feb 30th 2001) seems also to be accepted as a valid date, though Feb has only 28 or 29 days (leap years)...

Any workaround about this ?

Thanks in advance for your reply,
Johnny Driesen
Belgium

@jdriesen
Copy link
Author

Update...

Sorry, just woke up ... and realising now that I already asked you this Q a while ago ...
Appologies...
Remembering now you wrote me you're only working with regular expressions (so it's rather difficult to find out about leap years, etc...)

But this one seems to work (at least, for dd/MM/yyyy)
http://regexr.com/346hf

i'm certainly NO PRO with regExpr, so that's why I'm just suggesting this ...

Grtz,
Johnny

@ghiscoding
Copy link
Owner

Yes that's right, for the dates I only validate the structure of it and not necessarily the date itself. I am using regular expression indeed and if you do find a better regex, then I can replace it (which you did find).

Thanks for the link, I will take a look at it later after work, seems to work quite good at first look :)

@ghiscoding
Copy link
Owner

I took a look at it and even though it does cover well the calendar dates, it has flaws. Or at least, to my eyes... You can enter 1 or 2 digits (day, month) and enter 2 or 4 digits for the year. That is the part that I'm not crazy about, especially since all my error messages strictly says to put 2 digits at all time.
If you look at the page you sent me, they accept this:

01/1/01

This is of course January 1st of 2001. I don't know about you, but I wouldn't accept this in my applications. I find this error prone.

However, even if I'm not an expert on Regex either I tried to give it a shot and modify the expression to accept only 2 digits (dd/mm) and 4 digits (yyyy) and I came out with the following

/^(?:(?:31(\/|-|\.)(?:0[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)02\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{4})$/

It seems to work with these modifications, but I did really brief tests. You might want to do a bit deeper tests to make sure it's all good.

Other suggestions, while looking at the list of my current available Validators, I see that I have date_euro_long and date_euro_short but I don't simply have the date_euro. I could perhaps use date_euro for accepting any amount of digits, so dates like 01/1/01 would be valid using this new validator.

At the end, it would be nice converting this nice Regex (that you found) for US dates as well. Though it might be tricky, I think it's doable. even if I'm not a fan of US dates (I'm from Canada, we use metric but also US stuff since they are so close to us lol). My preferred date format is still ISO.

So that might take me a while to do all the changes. Feel free to comments.

@jdriesen
Copy link
Author

Hi Ghiscoding,

Thanks for your extended feedback. Highly appreciated !
Well, I'm living in Belgium and my application will have a roll-out in BE, NL, Germany and Luxembourg and France.

Lucky me, we agreed to to accept only long euro dates...
So, in a format of dd/mm/yyyy or dd-mm-yyyy

I certainly agree with you that I wouldn't accept 01/01/01 in any application. (But other devs maybe will accept this, or are having a reason to accept this in their application)

So I certainly like your idea of implementing a 'date_euro' as well.

As written before, I'm certainly NOT expert in RegEx, but if you can tell me how I can test them, I certainly willing to help you !
(I will do some more research about this topic by my own this weekend as well)

Keep in touch ! Promise !

PS.: it seems that your modified RegEx (accepting 4 digits in case of 2or4) seems to work fine (for my needs)
Can you please tell me how I can implement this in your code ? (or which is the best way to follow ?)

Thanks in advance for your reply and the great support !

Grtz,
Johnny

@ghiscoding
Copy link
Owner

Bonjour Johnny,

Thanks for the response, I think that I got Euro & US Dates working with this new way of validating (all base of the same regex you found). It was quite some work to modify it but it seems that I managed, I'll have to do more tests to make sure. However I can't implement it just yet as I would like to have all 3 type of dates working (Euro, US and ISO), so I'm only left with ISO to figure out. Hmm thinking about it, I actually have more work, since I have only done the long versions, I didn't do the short versions of each yet (short being just the year being 2 digits instead of 4).

Since I want to do all or nothing, it might be somewhere next week until I can publish a new version.

BTW, je suis au Qc, alors je parle Francais aussi ;) D'ailleurs c'est moi qui a fait les traductions francaise :)
A+

@jdriesen
Copy link
Author

jdriesen commented Sep 22, 2016

Hello Ghislain,

Well, I'm born and raised up in the Northern part of Belgium (Flanders), so my mother tongue is Dutch.
I'm understanding the French language, but I lost my 'writing' skills over the years :)

Thanks a lot for your effort about the date-'problem' !
Was talking with a friend about this subject, and he gave me 2 very interesting links...
First one is a series of 7 very good youtube vids

Second one is a link to a complete, 150 pages book

Gonna start studying now :)

Keep in touch,

Grtz,
Johnny

@jdriesen
Copy link
Author

BTW ...

This is an interesting Online RegEx Tester

Grtz,
Johnny

@ghiscoding
Copy link
Owner

Hi there, I made a new version 1.5.6 for this and also created 2 new validators (date_euro and date_us with of course all their min, max, between). These new one are accepting short/long year (that is 2 or 4 digits).

NOTE: I deprecated all the short versions, they can be use but I just didn't update them since I spent too much time on trying to adapt them to be 2 digits only (yy) and not accept (yyyy). Anyway, I don't really care about them since the other new validators accept both (yy or yyyy) on the same pass.

@ghiscoding
Copy link
Owner

ghiscoding commented Sep 24, 2016

BTW not sure if you did or not, but you can support by clicking on ⭐ (star on stop). I like to know many users have it installed.

Ohh and I don't believe that dutch is part of my locale list, is it? You could make a PR on that one ;)

Cheers
A la prochaine ;)

@jdriesen
Copy link
Author

Hi Ghislain,

Thanks for the update !
Really appreciating your effort.

Currently fighting with a deadline, but the Dutch translation will be there ASAP !
Promise.

Grtz,
Johnny

@jdriesen
Copy link
Author

Just contacted a friend of mine.
She's a legalized translator English-Romanian and English-Hungarian.
So also these translations will become available soon.

Grtz,
Johnny

@ghiscoding
Copy link
Owner

Sounds great, thanks :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants