[英]How can I create a number input that always pads to 2 digits?
I have two type="number"
<input>
elements, which I want to use to let people enter the hours and minutes parts of a time value in a mobile application.我有两个
type="number"
<input>
元素,我想用它们让人们在移动应用程序中输入时间值的小时和分钟部分。
By default, the two input element's value is 0
, so they both show 0
.默认情况下,两个输入元素的值为
0
,因此它们都显示0
。 This results in a display of 0:0
.这导致显示
0:0
。 The usual representation however would be 0:00
, so I would want the second input to show double digits.然而,通常的表示是
0:00
,所以我希望第二个输入显示两位数。
Also, if the user enters a value below 10, I would still want the input to show the number padded with a 0
.此外,如果用户输入的值低于 10,我仍然希望输入显示用
0
填充的数字。
I could imagine a couple of ways to do this with a text input field, but that would create the problem of validation.我可以想象几种使用文本输入字段执行此操作的方法,但这会产生验证问题。 Also, I picked
type="number"
specifically, so the mobile OS will pop open the numeric input when the input
element is focused.此外,我特别选择了
type="number"
,因此当input
元素被聚焦时,移动操作系统会弹出数字输入。
You can use these code for number pad :-您可以将这些代码用于数字键盘:-
function pad(n, width, z)
{
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
pad(10, 4); // 0010
pad(9, 4); // 0009
pad(123, 4); // 0123
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.