我想使用 joi 验证时间字段

I want to validate time field using joi

我已经采用了日期和时间输入字段。 我使用 joi 库进行输入验证是日期和时间不能单独为 null。 我成功地应用了日期验证,但我无法应用时间验证。 我怎样才能实现时间验证。

这是我的 html 日期和时间输入字段文件的代码。

html += '<td><input type="text" id="tier3City'+cnt+'"  readonly="readonly" name="tier3City" 
class="form-control" /></td>';
html += '<td><input type="date" id="fromDate'+cnt+'" name="fromDate" class="form-control" /></td>';
html += '<td><input type="time" id="fromTime'+cnt+'" name="fromTime" class="form-control" /></td>';
html += '<td><input type="date" id="toDate'+cnt+'" name="toDate" class="form-control" /></td>';
html += '<td><input type="time" id="toTime'+cnt+'" name="toTime" class="form-control" /></td>';

这是我的 ejs 文件代码。

  else if(stayOption == 'Stay')
  {
      schema=joi.object({
      stayOption:joi.string().required().label('Please Choose Stay Option'),
      projectTask:joi.string().required().label('Please select Activity Code.'),
      placeJourney: joi.string().required().label('Please select Place of Journey.'),
      fromDated:joi.date().required().label('Please select FROM(Departure Time from Residence)'),
      fromDat:joi.date().max('now').required().label('Please select FROM(Departure Time from Residence) less than or equals to Today'),
      fromTimes:joi.time().required().label('Please select FROM(Departure Time from Residence) Time'),
      toDated:joi.date().required().label('Please select TO(Arrival Time to Residence)'),
      toDate:joi.date().max('now').required().label('TO(Arrival Time to Residence must be less than or equals to Today'),
      fromDate:joi.date().required().less(joi.ref('toDate')).label('From(Departure Time from Residence) must be less than To (Arrival Time to Residence)'),
      toTimes:joi.time().required().label('Please select TO (Arrival Time to Residence)Time'),
      actualAMTForBL:joi.number().required().label('Please enter Actual Boarding lodging Amount'),
      imgpath:joi.string().invalid('demo').label('Please Upload File/Attachments').required(),
  })
  result=schema.validate({stayOption:stayOption,projectTask:projectTask,placeJourney:placeJourney,fromDated:fromDate,fromDat:fromDate,toDated:toDate,fromDate:fromDate,fromTimes:fromTime,toDate:toDate,toTimes:toTime,actualAMTForBL:actualAMTForBL,imgpath:imgpath});

您必须使用 RegEx 来验证时间(Joi.string().regex() 而不是 Joi.time())。

您的问题的解决方案是