简体   繁体   中英

How to tell if “123@231.23” is not a number in javascript?

parseInt("123@231.23") returns 123, which is a number.

There are tons of functions out there to detect if something is a number or not already, but they all depend on parseInt .

What is another generic way of detecting that this is not an integer without using regex?

if (isNaN("123@231.23"))
{
 alert("IsNaN - not a number");
}
else
{
 alert ("it is a number");
}

I'm assuming that OP need to distinguish if input is a number or not. If input is float or integer looks irrelevant to his problem. Maybe, I'm wrong...

EDIT: Alright, to keep everyone happy, integer in javasript is pretty big. How big integer is in javascript check here .

Asking if something is integer is asking is it a whole number between 9007199254740992 and -9007199254740992. Wholeness of the number you may check using modulus %

 $("#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"> 

Convert back to String and compare:

String(parseInt("123"))=="123" // true
String(parseInt("123.sdfs"))=="123.sdfs" //false

If you really want to check "for valid integer" you must combine isNaN with something else like this:

function isValidInteger(numberToTest) {
  return !(isNaN(numberToTest) || String(parseInt(numberToTest)) !== numberToTest.toString());    
}

This will evaluate like:

console.log(isValidInteger('123@231.23')); // false
console.log(isValidInteger('123231.23')); // false
console.log(isValidInteger('12323')); // true
console.log(isValidInteger(1e-1)); // false
console.log(isValidInteger('1e-1')); // false

And this work even with numbers. Here is PLNKR to test.

I think this is the best way to test for integers:

function isInt(str) {
    if (typeof str !== 'number' && typeof str !== 'string') {
        return false;
    }

    return str % 1 === 0;
}

Just note that strings / numbers like "123.0" evaluates to true .

Here's yet another one that doesn't rely on string stuff:

function looksLikeInteger(n) {
  return +n == n && +n === ~~n;
}

Probably should be called "looksLikeJavaScriptInteger" because it only works for 32-bit integers. It coerces to numeric with unary + and then checks for equality (so ugly strings and objects are tossed out there) and then checks to make sure that the numeric value doesn't change when coerced to an integer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM