Verified regular expressions return always false when used with the regex validation rule

Hello,

I have been using the following regular expression, ^(0\d{1}|1[0-2])\/([0-2]\d{1}|3[0-1])\/(19|20)\d{2} ?((0[0-9]|1[0-2]):[0-5][0-9] ([A]|[P])[M])?$ to validate a date with an optional time component. It verifies correctly test stings such as 12/02/2022 and 12/02/2022 00:00 AM in regex101, but when used in the regex validation rule, it always returns false.

https://stackblitz.com/edit/react-eg4tzs2h?file=App.js


4 Replies

PK Priyanka Karthikeyan Syncfusion Team January 14, 2025 11:59 AM UTC

Hi George,

 

Thank you for reaching out to us! We truly appreciate your inquiry. To assist you with your requirement, you can use the following regular expression. This will help ensure that the date is validated in the desired MM/DD/YYYY format, with an optional time component in HH:MM AM/PM format. Below is the code snippet that incorporates this regular expression along with the appropriate validation rules.

 

 

  const customerIDRules = {regex: [

    new RegExp(

      '^(0\\d|1[0-2])\\/([0-2]\\d|3[0-1])\\/(19|20)\\d{2}(\\s(0[1-9]|1[0-2]):[0-5]\\d\\s[AP]M)?$'

    ),

    'Enter a valid date in MM/DD/YYYY or MM/DD/YYYY HH:MM AM/PM format.',

  ]};

 

Sample: https://stackblitz.com/edit/react-eg4tzs2h-po6vodxr?file=App.js

 

Regards,

Priyanka K

 

 



GE George January 14, 2025 01:02 PM UTC

Τhank you very much.



GE George January 14, 2025 01:03 PM UTC

Τhank you very much.



PK Priyanka Karthikeyan Syncfusion Team January 15, 2025 03:30 AM UTC

Hi George,



You are welcome. Please feel free to contact us if you have any further questions.

 

Regards,

Priyanka K



Loader.
Up arrow icon