'validate hour field with yup

I have field hour declared in material-ui TextField. I want to add Yup validation to control the format for the value typed which should respect this format: 02:30

How can I control it with Yup?



Solution 1:[1]

You can use regex to achieve your goal. I made in this way:

const schema = yup.object({
  hour: yup
    .string()
    .length(5)
    .matches(/(\d){2}:(\d){2}/, 'Hour must have this pattern "00:00"'),
});

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 Renato Willyan