I'm attempting to use Pure Javascript (no jquery if possible).
I need to update my input boxes' value so that it looks like this: 01
. My usecase is for a MM and DD input boxes (mainly for aesthetics). When using the input wheel it will increase with 1
, 2
... I would like it to increment like this: 01
, 02
...
Asked
Active
Viewed 1,378 times
1

hhsnopek
- 364
- 3
- 18
-
2Can you provide the code that you've tried for us to look at? – talemyn Jun 08 '15 at 21:20
-
Doesn't seem to be a great user experience as you click the wheel, but throwing something on the `change` event of the textbox to change the textbox value seems to work. – Joe Enos Jun 08 '15 at 21:24
-
input type of number can not have a leading zero. http://stackoverflow.com/questions/8437529/html5-input-type-number-removes-leading-zero – epascarello Jun 08 '15 at 21:24
-
Currently I don't have any code - my previous attempt was to just prepend a `0`. I know it's not suppose to but after thinking about it I could parse the `0` out of it then update it manually... but that's just a huge pain – hhsnopek Jun 08 '15 at 21:25
-
@hhsnopek here you go, the general idea is there : http://stackoverflow.com/questions/7790561/how-can-i-make-the-html5-number-field-display-trailing-zeroes – Daemedeor Jun 08 '15 at 21:26
-
It's very simple. If the number is less than 10, prepend a zero. – enhzflep Jun 08 '15 at 23:06
-
@enhzflep its simple in some ways, if that was what took, however, most browsers format the input type of number to remove the 0, so you'll have to do some fancy changing of the input type as he eventually did – Daemedeor Jun 09 '15 at 02:42
1 Answers
1
Thank you @Daemedeor for leading me to this:
This will change the type to text
and prepend the 0
on blur. On focus it will change type text
back to type number
and remove the leading 0
. All you must do is style the inputs so they don't change widths.
(function() {
var monthInput = document.getElementById('month');
var dayInput = document.getElementById('day');
var prependZero = function() {
if (this.value === '') return;
event.target.setAttribute('type', 'text');
if (this.value.indexOf('0') === -1 && this.value.length !== 2) this.value = '0' + this.value;
};
var removeZero = function() {
this.value = this.value.split('0')[1];
this.setAttribute('type', 'number');
};
dayInput.addEventListener('blur', prependZero, false);
dayInput.addEventListener('focus', removeZero, false);
monthInput.addEventListener('blur', prependZero, false);
monthInput.addEventListener('focus', removeZero, false);
})();

hhsnopek
- 364
- 3
- 18
-
careful though, looking at the function, it seems that you might want to check the length, make sure its not like 10/11/12 or any number with a length of 2 or greater because that might also be undesirable – Daemedeor Jun 08 '15 at 22:34
-
Yeah, I ran into that problem locally and fixed it but didn't update it here. I've updated it now, thanks for the reminder :) – hhsnopek Jun 09 '15 at 01:35
-
I realize you are only asking for month and date, but if your input doesn't have a max and min attribute, don't forget to also make sure the length is not greater than 2 instead of just two ~ in case users go past the 30 days – Daemedeor Jun 09 '15 at 02:44
-
In that case I added listeners to just notify that the inputs were invalid, with UI/UX – hhsnopek Jun 09 '15 at 02:59