I am currently developing a part of a web app that takes a 4-digit pin input.
I use a piece of logic to check both the PIN input and PIN conformation input are at least 4 digits and they are the same.
Here is my code:
<md-input-containe>
<label>New 4-digit PIN</label>
<input ng-model="selectedDriver.password" type="number"
ng-pattern="/^\d{4}$/" required>
</md-input-container>
<md-input-containe>
<label>Confirm the PIN</label>
<input ng-model="selectedDriver.passwordConfirm" type="number"
ng-pattern="/^\d{4}$/" required>
</md-input-container>
<md-button class="md-raised" ng-click="saveDriver()"
ng-disabled="selectedDriver.password != selectedDriver.passwordConfirm
|| selectedDriver.password.toString().length != 4">
The main part to take note of is this:
ng-disabled="selectedDriver.password != selectedDriver.passwordConfirm || selectedDriver.password.toString().length != 4"
The problem is, say a pin such as 0123 is entered, .toString() turns it into "123" because it removes the needless leading zeroes so it then fails the length check.
What are some possible solutions? I looked at padding but it doesn't seem to be dynamic enough for what I need, I need a simple function that will take any input like 0012 and turn it into "0012".
A possible solution would be to use text inputs and have some ng-change logic that would block any non-numeric characters being typed but I don't believe that this is the right solution.
Thanks for your help.
EDIT: Apparently I need to explain why this is different to a "padding" issue. This is different because the user's input could be something like '38', if it was '38' I wouldn't want to mindlessly pad it to '0038', it should only be '0038' if they have typed that. I am not somebody who posts on here without scouring the web and trying every existing solution first.
The accepted answer is very useful to other SO users as there is no other one like it on here.