gpt4 book ai didi

Javascript 代码没有捕获空值

转载 作者:行者123 更新时间:2023-11-30 12:11:02 25 4
gpt4 key购买 nike

我真的是 javascript 的新手,我试图阻止文本框接受字母和空值。这是我的:

function checkInp()
{
var x=document.forms["newPerson"]["ThisBox"].value;
if (isNaN(x) || is_null(x))
{
alert("This box can only contain numeric values");
return false;
}
}

这是我的表单标签:

<form name="newPerson" action="newPerson2.php" method="POST" onsubmit="return checkInp();">

这不会阻止字母通过,但不会阻止空值通过。

最佳答案

尝试这样检查

if(!x || isNaN(x)){
alert("This box can only contain numeric values");
return false;
}

在 javascript "",null,undefined,NaN,0 中都认为 false

关于Javascript 代码没有捕获空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33794213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com