允许用户输入大于 0 的数字“和/或”最多 2 位小数。
预期输出格式:
123.45
或
1.46
或
34234.23
或
34.04
或
0.33
不喜欢:
67.6787
或
45.546.65
或
0
下面的代码工作正常,但它接受“0”
$('input').keyup(function () {
if(!(/^\d+(\.{0,1}\d{0,2})?$/.test(this.value)))
this.value = this.value.substring(0, this.value.length - 1);
});
HTML
<table>
<tr>
<td>
<asp:TextBox ID="txt1" runat="server" Width="50"/>
</td>
</tr>
</table>
最佳答案
您可以使用基于负向前瞻的正则表达式来禁止 0 值:
/^(?!0(\.0*)?$)\d+(\.?\d{0,2})?$/
它将允许:
0.1
1.0
345.67
1.1
1.23
它不允许:
0.
0
0.0
0.00
45.56.45
关于jquery - 不允许使用正则表达式为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28660632/