I want to validate time field using joi

Viewed 941

I have taken date and time input field. I have used joi library for input validation is that date and time can not be null seperately. I am succesfull in applying date validation but i'm unable to apply time validation. How can i achieve time validation.

Here is my code for html file for date and time input field.

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>';

Here is my code for ejs file.

  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});
1 Answers

You have to use RegEx to validate time(Joi.string().regex() instead of Joi.time()).

The solution to your problem is here.

Related