Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.

Javascript Date Format Change on exit

Avatar

Level 2

My date field has MM/DD/YYYY format which would show 08/03/2017 for example. If I copy a date from a website as 8/3/2017 and paste into the field I would like the date field to recognize that format and change to 08/03/2017 and not throw up a validation error. I looked at various ways to this but can't figure it out through the patterns (to accept the 8/3/2017 format) or through javascript in the exit event.

2 Replies

Avatar

Level 10

You can combine multiple patterns when setting up the edit pattern to allow several different ways for entering a date.

Edit pattern to allow 08/03/2017 and 8/3/2017

date{MM/DD/YYYY}|date{M/D/YYYY}

Avatar

Level 1

break the date string first then take the month as an integer.

example.
Compare with 12. If it is greater than 12 then it must be a date, not a month.
Then just change the format as per your need
See how to change the date format

https://www.codespeedy.com/format-javascript-date-to-yyyy-mm-dd/ 

But in your case, you can take multiple date pattern so that any date pattern will be valid to pass the validation check.