What we are looking to do is, have an input that only accepts 0 - 24 (for a time entry application).
These are the values the user should be able to enter into the 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 is as follows:
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;
};
};
This works fine for limiting the numbers, but it doesn't prevent the user from entering unlimited numbers of preceding 0s, which we don't want.
I tried using time.toString().length and adding some validation using that, but turns out, 00000 as a number, when set to string still becomes "0".
Is there a better way to limit the amount of digits to 2?
Thanks muchly.
ng-maxlength, but it doesn't seem to work, still lets the user enter as many numbers as they want :/ng-maxlengthagain. It works, but whenever you exceed 2 digits, instead of blocking further input (which would be ideal) it simply resets the field. Which I suppose is a solution we can work with. Thanks.