Number : <input type="text" name="inputId" id="quantity" maxlength="11" />
当用户输入时应满足以下条件 1. 值可以是正数、负数或 float ,最大长度为11
这就是我到目前为止所写的内容
$(document).ready(function () {
//called when key is pressed in textbox
$("#inputID").keypress(function (e) {
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57 || e.which == 45 )) {
//display error message
if(e.which == 45) {
var value = $("#quantity").val();
if(value.indexOf('-') != -1)
{
var index = value.lastIndexOf('-');
if(index = 0){
return false;
}
}
}
else
{
$("#quantity").val("-");
}
}
});
});
我没有考虑“。”截至目前,只有负号应该位于索引零。
有没有更好的方法来做到这一点或修改此代码将是正确的方法。
请提出建议。
最佳答案
可能最简单的方法是使用 HTML5 验证:
<body>
<form>
<input type="text" maxlength="11" pattern="\-?\d+\.?\d+" />
<input type="submit" value="Submit" />
</form>
</body>
关于javascript - 输入文本框为正数/负数/ float ,最大长度为 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29938302/