I need a regular expression to check the duration in ISO 8601 format (except for the fractional parts that I don't need).
PnYnMnDTnHnMnS
PNW
Here is what I have:
^P(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(\d+H)?(\d+M)?(\d+S)?)?$
The only problem is that strings with constants P and PT allowed with this regex, since all parts are "zero or one" ? .
- There must be at least one component (date or time)
- If
T exists, then there must be a time component (H, M or S) - If
T exists, then there may or may not be any date components (Y, M, or D) - Overflow allowed (e.g.
P72H is basically equivalent to P3D )
Valid inputs:
P1Y // date component only P2MT30M // date and time components PT6H // time component only P5W // another date component
Inappropriate Inputs:
P // no components PT // no components P3MT // T specified but not time components
Invalid strings are currently being tested on the client side, but do not work on the server side, because they are passed to DateInteval , d as a failure on the client side, if possible. If everyone used Chrome 40+, I could point minlength='3' to the input element to help, but unfortunately this is not the case.
source share