parseInt("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ae9f9c9dee9c9d9f809c9d" rel="noreferrer noopener nofollow">[email protected]</a>")
返回 123,这是一个数字。
有大量的函数可以检测某个东西是否是数字,但它们都依赖于 parseInt。
在不使用正则表达式的情况下检测这不是整数的另一种通用方法是什么?
最佳答案
if (isNaN("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4f5f6f784f6f7f5eaf6f7" rel="noreferrer noopener nofollow">[email protected]</a>"))
{
alert("IsNaN - not a number");
}
else
{
alert ("it is a number");
}
我假设OP需要区分输入是否是数字。如果输入是 float 或整数看起来与他的问题无关。 也许,我错了......
编辑: 好吧,为了让大家高兴,javasript 中的整数相当大。 javascript检查中的整数有多大here .
询问某物是否为整数是询问它是否是 9007199254740992 和 -9007199254740992 之间的整数。您可以使用模数 %
$("#cmd").click(function (e) { ChectIfInteger( $("#txt").val() ) });
function ChectIfInteger(myval){
if (isNaN(myval)){
alert("not integer (not number)")
}
else{
//it is a number but it is integer?
if( myval % 1 == 0 ){
if (myval <= 9007199254740992 && myval >= -9007199254740992)
{
alert("it is integer in javascript");
}
else{
alert ("not integer");
}
}
else{
alert("nope, not integer");
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txt"/>
<input type="button" id="cmd" value="test input">
关于javascript - 如何判断 "123@231.23"不是 JavaScript 中的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31638044/