Javascript 防止启动模式打开,并在无效输入上提供错误消息
如果我将所有无效输入设置为:Javascript 防止启动模式打开,并在无效输入上提供错误消息,javascript,validation,input,Javascript,Validation,Input,如果我将所有无效输入设置为: const isOpen = (val) => { const num = parseFloat(val); if (isNaN(num)) { return true; } else { const decimals = num.toString().split('.')[1] || ''; if (decimals.length > 2) { return true; } else {
const isOpen = (val) => {
const num = parseFloat(val);
if (isNaN(num)) {
return true;
} else {
const decimals = num.toString().split('.')[1] || '';
if (decimals.length > 2) {
return true;
} else {
return false;
}
}
};
这表明,如果输入为NaN或小数位数过多,则这是无效的。但我还想补充一点,如果其中一个条件为真,这种类型的线会阻止模态打开:
$("#dzCalculator").on("submit", function(e) {
$("#dilemma_zone_output").modal("show");
e.preventDefault();
});
如果其中一个是真的,我如何将其添加到特定代码中以防止模式打开
发生的第二件事是弹出一个错误消息:
$(document).ready(function() {
$("#submitCalculation").click(function() {
$(".checkVelocity").each(function() {
const val = $(this).val();
if (isOpen(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");
$(this).click(function() {
$(this).popover("hide");
});
}
})
})
})
$(文档).ready(函数(){
$(“#提交计算”)。单击(函数(){
$(“.checkVelocity”)。每个(函数(){
const val=$(this.val();
if(等参(val)){
$(这个)({
位置:“左”,
内容:“”,
模板:“”+
'' +
'输入车速在10到300 ms-1、kmh-1或mph之间。'+
''
});
$(此).popover(“显示”);
$(此)。单击(函数(){
$(此).popover(“隐藏”);
});
}
})
})
})
因此,如果上述条件之一为真,则模态不会打开,并显示错误弹出框
这一切都来自我这样做的输入:
<input
type="text"
inputmode="decimal"
id="length"
class="reset_form checkLength"
value="4.5"
autocomplete="off"
aria-label="input value for length of vehicle"
required>
你可能会说-只要使用input type=“number”-我可以,而且我已经做到了。但这显然还不够好,因为Chrome阻止了任何NaN键——我有所有的代码,我现在只需要想办法把它们粘在一起