Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
136
rated 0 times [  138] [ 2]  / answers: 1 / hits: 17008  / 6 Years ago, tue, july 10, 2018, 12:00:00

I want to validate DateTime like this code below



{
data: {
start: 2018-05-12 08:00:00
}
}


How to combine isISO8601() and match(regex) to validate date & time in start



body('*.start')
.exists()
.not()
.isEmpty()
.withMessage('start cannot be empty')
.isISO8601('yyyy-mm-dd')
.matches('^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$')
.withMessage('start must be in correct format yyyy:mm:dd hh:mm:ss'),

More From » node.js

 Answers
3

Unfortunately having datetime validation is currently unavailable with express-validator.



Meanwhile you can go for a pure regex, which will be,



.matches('/^([+-]?d{4}(?!d{2}b))((-?)((0[1-9]|1[0-2])(3([12]d|0[1-9]|3[01]))?|W([0-4]d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]d|[12]d{2}|3([0-5]d|6[1-6])))([Ts]((([01]d|2[0-3])((:?)[0-5]d)?|24:?00)([.,]d+(?!:))?)?(17[0-5]d([.,]d+)?)?([zZ]|([+-])([01]d|2[0-3]):?([0-5]d)?)?)?)?$/)


Hope this helps!


[#54013] Friday, July 6, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kinsleyashlynnh

Total Points: 64
Total Questions: 119
Total Answers: 98

Location: Burundi
Member since Sat, Aug 21, 2021
3 Years ago
;