'how can I validate a date of birth using Regular Expression (RegExp), in a react form?

const dateRegex = new RegExp('/^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$/')
        if (!formData.dob || !dateRegex.test(formData.dob)) {
            formErrors.dob = "date of birth is required"
        }

The RegExp I am using for the date of birth isn't working. Can someone help me with this, please?

regex



Solution 1:[1]

Try using this RegEx

^(?:0[1-9]|[12]\d|3[01])([\/.-])(?:0[1-9]|1[0-2])\1(?:19|20)\d\d$

You can read more about this regex in this post: Date of birth validation by using regular expression

Checkout Coenwulf's answer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cores13