Time validation pattern for Angular / Javascript


Time validator in Angular and Javascript using RegExp patterns

We want to validate an input, the required format is hh:mm

Examples:

8:59 AM = 08:59

8:59 PM = 20:59

Javascript regular expression

/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/ 

Angular form

formControl = new FormControl('', 
 Validators.pattern(/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/)); 

Notice that we are not using a string but a JS/TS RegExp