【发布时间】:2015-04-29 08:29:50
【问题描述】:
Number : <input type="text" name="inputId" id="quantity" maxlength="11" />
当用户输入时,它应该满足以下条件 1. 值可以是正数或负数或浮动,最大长度为 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("-");
}
}
});
});
我没有考虑“。”到目前为止,只有负号应该在索引零处。
有没有更好的方法来做到这一点,或者修改这段代码是正确的方法。
请提出建议。
【问题讨论】:
-
在提交时进行检查会比在按键时更简单吗?
-
这是 req.用户将无法进入
-
@evolutionxbox 对提交的检查将对用户非常不友好。从 UX 角度来看,建议进行实时错误或验证检查
标签: javascript jquery input