简体   繁体   English

如何在小于 10 的数字前插入零? HTML 输入框

[英]How can I insert a zero in front of a number less than 10? HTML Input Box

I created an html input box where I would like a person to be able to enter at most 2 characters numbers (therefore from 01 to 99) and if the number is less than 10 the box must add zero by itself.我创建了一个 html 输入框,我希望一个人最多可以输入 2 个字符的数字(因此从 01 到 99),如果数字小于 10,则该框必须自行添加零。

There are 2 problems:有2个问题:

  1. If I type 00 , the script writes 000 ;如果我输入00 ,脚本会写000 if I write 01 the script writes 001 , etc.如果我写01脚本写001等。
  2. The box accepts numbers over 99盒子接受超过 99 的数字

Code:代码:

<input type="number" class="form-control" name="amount_centesimi" min="0" max="99" value="00" onchange="if(parseInt(this.value,10)<10)this.value='0'+this.value;" required>

try this.尝试这个。

<input type="number" class="form-control" name="amount_centesimi" min="0" max="99" value="00" onchange="this.value=(parseInt(this.value)<10)?('0'+parseInt(this.value)):parseInt(this.value)" style="width: 100px" required>

There was two problem in your code:您的代码中有两个问题:

if (parseInt(this.value,10) < 10) this.value= '0' + this.value;

1 You missed to sanitize the value when you assign it to this.value . 1 将值分配给this.value时,您错过了对其进行清理。

You write code that this.value = '0' + this.value;你写的代码this.value = '0' + this.value;

So, if your input value was '01' , js use the raw string value and make it '001' ( '0' + '01' )因此,如果您的输入值为'01' ,则 js 使用原始字符串值并将其'001' ( '0' + '01' )

You had to sanitize the value like this:您必须像这样清理值:

this.value = '0' + parseInt(this.value)

2 You had to change the value even if the input number was greater than 10 2 即使输入数字大于 10,您也必须更改该值

Or, if you input value like '000999', it will not changes to '999'或者,如果您输入类似 '000999' 的值,它不会更改为 '999'

I would use padStart :我会使用padStart

<input 
   type="number"
   class="form-control"
   name="amount_centesimi"
   min="0"
   max="99"
   value="00"
   onchange="this.value = this.value.padStart(2, '0')"
   required />

Not sure if I'm doing it right, but when I used onChange it didn't update the value instantly, so I've used onInput .不确定我是否做得对,但是当我使用onChange它没有立即更新值,所以我使用了onInput

Since I couldn't think of a way to track the previous value it will roll over if you keep typing numbers.由于我想不出一种方法来跟踪以前的值,如果您继续输入数字,它会翻转。

  • Type 0 - get 00输入 0 - 得到 00
  • Type 1 - get 01类型 1 - 得到 01
  • Type 1 - get 11类型 1 - 得到 11
  • Type 2 - get 12类型 2 - 得到 12
  • Type 3 - get 23类型 3 - 得到 23

 <input min="0" max="99" value="00" type="number" oninput="(function(e) { let value = e.value.replace(/\\D/g,''); if (parseInt(value, 10) < 10) { value = '0' + value; } e.value = value.substr(-2); })(this);" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以隐藏 HTML5 数字输入的旋转框吗? - Can I hide the HTML5 number input’s spin box? 如何在输入框中仅输入有效的电话号码? - How can i input only valid telephone number in input box? javascript 输入秒数 - 不小于零 - javascript enter number of seconds - not less than zero 如何验证电话号码不包含任何其他字符,包括 - 和。 在 html 页面中使用 javascript,不应少于 10 位 - How to validate a phone number doesn't contain any other characters including - and . using javascript in a html page, shouldnt be less than 10 digits 当我 select 输入框时,如何将“您正在输入数字”放在输入框的正下方? - How can i put "you are entering a number" right below the input box when i select input box? 如何隐藏小于输入数字的li元素 - How to hide elements of li with values less than input number 如何使用Regexp在Javascript中将任意数字前面的零前置零 - How to prepend a zero in front of any number below 10 in Javascript using Regexp 如何仅在 HTML/JQuery 中获取从 1 到 10 的输入? - How can I take input from 1 to 10 only in HTML/JQuery? 如何使用 jQuery 在输入框或 html 元素中获取输出? - How can I get output in input box or html element with jQuery? 如果小时数小于 10,JavaScript 会在小时前显示一个零 - JavaScript display hour with a zero before it if it is less than 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM