Our goal is to create an input field that only allows values from 0 to 24 (for a time entry application).
These are the specific values users should be able to input:
0
1
2
3
4
5
6
7
8
9
00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
The current code snippet looks like this:
HTML:
<div class="list list-inset">
<label class="item item-input">
<input id="time" type="number" placeholder="24" ng-model="$parent.time1" ng-change="onChange(time1)">
</label>
<label class="item item-input">
<input type="tel" placeholder="24" ng-model="time2" >
</label>
</div>
JS/Angular:
var savedTime = 0;
$scope.onChange = function(time) {
if (time > 23 || currentTime > 2) {
$scope.time1 = savedTime;
} else {
savedTime = time;
};
};
While this restricts the input numbers, it does not prevent users from entering multiple preceding zeros, which is not desired.
I attempted using time.toString().length
and implementing some validation based on that, but discovered that even when converting 00000
to a string, it still results in "0"
.
Is there a more effective way to limit the input to two digits?
Thank you.