【发布时间】:2021-01-02 09:00:06
【问题描述】:
我正在检查有效输入并成功检查了空白输入,但是如何使用 jquery 检查小数位的有效长度?
if ($val === "")
这对于空白值很好,但是找到不超过 2 位小数的输入的正确语法是什么?
这是目前为止的代码,浏览器现在正在恢复到本机浏览器弹出窗口,以在输入时发出警告。
$(document).ready(function() {
$("#submitCalculation").click(function() {
$(".checkVelocity").each(function() {
$val = $(this).val();
if ($val === "") {
if ($val.split(".")[1] || "").length > 2) {
$(this).popover({
placement: "left",
content: '<textarea class="popover-textarea"></textarea>',
template: '<div class="popover"><div class="arrow"></div>'+
'<div class="row"><div class="col-3 my-auto"><i class="fas fa-exclamation-triangle" id="invalid-input3">'+
'</i></div><div class="popover-content col-9">Enter the velocity of the car between 10 and 300 ms<sup>-1</sup>, kmh<sup>-1</sup> or mph.'+
'</div></div>'
});
$(this).popover('show');
}}
})
})
})
最初它适用于空白或不正确的非数字输入:
$(document).ready(function() {
$("#submitCalculation").click(function() {
$(".checkVelocity").each(function() {
$val = $(this).val();
if ($val === "") {
$(this).popover({
placement: "left",
content: '<textarea class="popover-textarea"></textarea>',
template: '<div class="popover"><div class="arrow"></div>'+
'<div class="row"><div class="col-3 my-auto"><i class="fas fa-exclamation-triangle" id="invalid-input3">'+
'</i></div><div class="popover-content col-9">Enter the velocity of the car between 10 and 300 ms<sup>-1</sup>, kmh<sup>-1</sup> or mph.'+
'</div></div>'
});
$(this).popover('show');
}
})
})
})
但它不适用于小数位数超过 2 的情况。
【问题讨论】:
-
对不起,我在问题中犯了一个错误,意思是说不超过2 dp。所以我不介意 2、2.2 或 2.21,但不介意 2.201(尾随零在 jquery 中并不重要,因此它们无论如何都不算数,你可以写 2.00000000000000000 但它只会读为 2。
标签: jquery validation input