Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joi validating time field

There is object with a property time (22:30:00).

const schema = Joi.object.keys({
  ...
  transactionDate: Joi.date().required(),
  transactionTime: Joi.time().required(), // ???
  ...
});

How to validate a time field using Joi?

like image 906
user2473015 Avatar asked Nov 29 '18 18:11

user2473015


1 Answers

Try this way

const schema = Joi.object().keys({
   ...
   transactionDate: Joi.string().regex(/^([0-9]{2})\:([0-9]{2})$/)
})

Hear I have used simple regex format.

You can also use this : ^([01]\d|2[0-3]):?([0-5]\d)$

for AM and PM \b((1[0-2]|0?[1-9]):([0-5][0-9])([AaPp][Mm]))

AM PM

like image 110
Sachin Shah Avatar answered Nov 02 '22 09:11

Sachin Shah