-1

I'm using some masked input plugin, which supports mask as the RegEx array. In my case, I want to validate 24h time format (hh:mm). I'm using now RegEx like this:

[/^([0-2])/, /([0-9])/, ':', /[0-5]/, /[0-9]/]

First 2 array elements represents hours

/^([0-2])/, /([0-9])/

Any suggestions how to validate second hour digit? Cause now 25:00 is valid but shouldn't. Need somehow to check first number and if it's 2 - replace [0-9] with [0-3].

Liam
  • 27,717
  • 28
  • 128
  • 190
demkovych
  • 7,827
  • 3
  • 19
  • 25

1 Answers1

0

Something like this would be more flexible

const arr = ["07:00", "25:05", "23:59", "00:00"];
const valid = arr.filter(t => {
  const parts = [hh, mm] = t.split(":");
  return !isNaN(hh) && +hh >= 0 && +hh < 24 && !isNaN(mm) && +mm >= 0 && +mm < 60;
});
console.log(valid.length, arr.length, valid.length === arr.length)
mplungjan
  • 169,008
  • 28
  • 173
  • 236