我有一个表格,我想添加clear button到我的inputs type="number"
所以我想我会沿着使用 a 的路线走下去regex,因为我不想button在 the 的末尾创建一个input,因为我的其他人都没有input人都没有这个。
我的下面的代码只允许“数字”和一位小数,但我需要限制小数点后的位数,这是我失败的地方。
<input type="text" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1');" />
Run Code Online (Sandbox Code Playgroud)
我知道我需要使用,{0,2}但我尝试了以下方法,但它们仍然允许小数点后 3 位以上的数字
/[^0-9.{0,2}]/g
/[^0-9.]{0,2}/g
Run Code Online (Sandbox Code Playgroud)
其他所有字符都没有键入,input我想将其保留为但我不希望使用一大块代码keyCode。
我不擅长regex它们,也很难理解它们。
我也尝试过:
<input type="search" oninput="this.value = this.value.replace(^\d+\.\d{1,2}$, '').replace(/(\..*?)\..*/g, '$1');" />
Run Code Online (Sandbox Code Playgroud)
你能做的就是添加.replace(/(\.\d{2}).+/g, '$1');到按钮。
输入小数点和两位数字后,它会替换所有内容:
<input type="text" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1').replace(/(\.\d{2}).+/g, '$1');" />Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1551 次 |
| 最近记录: |