0

I have a text field in the Sitecore form where the user needs to enter a Date. Currently, I am using Regex Validator that check date in mm/dd/yyyy format.

Need support to check if the date is in yyyy/mm/dd format.

{"regularExpression":"^((0[13578]|1[02])[/]31[/](18|19|20)[0-9]{2})|((01|0[3-9]|1[1-2])[/](29|30)[/](18|19|20)[0-9]{2})|((0[1-9]|1[0-2])[/](0[1-9]|1[0-9]|2[0-8])[/](18|19|20)[0-9]{2})|((02)[/]29[/](((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))$"}

The above regex I am currently using.

6
  • Welcome to SSE @Abhinav. Can you please elaborate on a problem you are facing ? Is it that none regex works? Or one works and other doesn't... Oct 9, 2023 at 5:28
  • The above regex validate date in mm/dd/yyyy format and is working as expected. I need support to validate in yyyy/mm/dd format and need regex for that.
    – Abhivav
    Oct 9, 2023 at 5:31
  • Btw I faced a similar issue with value not being validated against regex. You can take a look at it as well. sitecore.stackexchange.com/questions/35323/… Oct 9, 2023 at 5:39
  • Checked the above links. The regex is working for me in mm/dd/yyyy format. But I am not able to modify regex to check date is in yyyy/mm/dd format.
    – Abhivav
    Oct 9, 2023 at 5:45
  • Apologies, I misread. Deleted that comment. Please check this link - stackoverflow.com/questions/5247219/… Oct 9, 2023 at 5:48

1 Answer 1

0

You just need to reorder regex parts like that:

{"regularExpression":"^((18|19|20)[0-9]{2}[/](0[13578]|1[02])[/]31)|((18|19|20)[0-9]{2}[/](01|0[3-9]|1[1-2])[/](29|30))|((18|19|20)[0-9]{2}[/](0[1-9]|1[0-2])[/](0[1-9]|1[0-9]|2[0-8]))|((((18|19|20)(04|08|[2468][048]|[13579][26]))|2000)[/](02)[/]29)$"}

Now year goes first, then month and then day.

2
  • It worked. Thanks.
    – Abhivav
    Oct 9, 2023 at 6:55
  • Then you can mark the answer as correct.
    – Marek Musielak
    Oct 9, 2023 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.