javascript - How to add `0` this kind of condition in code? -
i have code below working fine trying if input 5:30
code auto add 0
output 05:30
.
i try edit code jsfiddle 2
failed it.
is possible want output be?.
html :
<div> <input id="departure" type="text" style="width: 50px; text-align: center" placeholder="00:00" maxlength = "5" /> </div>
javascript :
<script type="text/javascript"> document.getelementbyid('departure').addeventlistener('keyup', function(event) { if(event.target.value.length === 2) { if((event.key && !isnan(event.key)) || (event.keycode >= 48 && event.keycode <= 57)) { if(!isnan(event.target.value) && parseint(event.target.value) > 12) { event.target.value = event.target.value.slice(0,1) + ':' + event.target.value.substring(1); } else { event.target.value += ':'; } } } }); </script>
you can try this:
'0' + event.target.value.slice(0,1).slice(-2) + ':' + '0' + event.target.value.substring(1).slice(-2)
ie., can pad 0 @ beginning of value , slice 2 digits last.
Comments
Post a Comment