(Built-in) way in JavaScript to check if a string

2018-12-31 08:47发布

I'm hoping there's something in the same conceptual space as the old VB6 IsNumeric() function?

25条回答
骚的不知所云
2楼-- · 2018-12-31 09:43

Well, I'm using this one I made...

It's been working so far:

function checkNumber(value) {
    if ( value % 1 == 0 )
    return true;
    else
    return false;
}

If you spot any problem with it, tell me, please.

查看更多
君临天下
3楼-- · 2018-12-31 09:43

Here's a one-liner to check if sNum is a valid numeric value; it has been tested for a wide variety of inputs:

!isNaN(+s.replace(/\s|\$/g, ''));  // returns True if numeric value
查看更多
只若初见
4楼-- · 2018-12-31 09:44

Try the isNan function:

The isNaN() function determines whether a value is an illegal number (Not-a-Number).

This function returns true if the value equates to NaN. Otherwise it returns false.

This function is different from the Number specific Number.isNaN() method.

  The global isNaN() function, converts the tested value to a Number, then tests it.

Number.isNan() does not convert the values to a Number, and will not return true for any value that is not of the type Number...

查看更多
人间绝色
5楼-- · 2018-12-31 09:45

I like the simplicity of this.

Number.isNaN(Number(value))

The above is regular Javascript, but I'm using this in conjunction with a typescript typeguard for smart type checking. This is very useful for the typescript compiler to give you correct intellisense, and no type errors.

Typescript typeguards

isNotNumber(value: string | number): value is string {
    return Number.isNaN(Number(this.smartImageWidth));
}
isNumber(value: string | number): value is number {
    return Number.isNaN(Number(this.smartImageWidth)) === false;
}

Let's say you have a property width which is number | string. You may want to do logic based on whether or not it's a string.

var width: number|string;
width = "100vw";

if (isNotNumber(width)) 
{
    // the compiler knows that width here must be a string
    if (width.endsWith('vw')) 
    {
        // we have a 'width' such as 100vw
    } 
}
else 
{
    // the compiler is smart and knows width here must be number
    var doubleWidth = width * 2;    
}

The typeguard is smart enough to constrain the type of width within the if statement to be ONLY string. This permits the compiler to allow width.endsWith(...) which it wouldn't allow if the type was string | number.

You can call the typeguard whatever you want isNotNumber, isNumber, isString, isNotString but I think isString is kind of ambiguous and harder to read.

查看更多
何处买醉
6楼-- · 2018-12-31 09:45

In my application we are only allowing a-z A-Z and 0-9 characters. I found the answer above using " string % 1 === 0" worked unless the string began with 0xnn (like 0x10) and then it would return it as numeric when we didn't want it to. The following simple trap in my numeric check seems to do the trick in our specific cases.

function isStringNumeric(str_input){   
    //concat a temporary 1 during the modulus to keep a beginning hex switch combination from messing us up   
    //very simple and as long as special characters (non a-z A-Z 0-9) are trapped it is fine   
    return '1'.concat(str_input) % 1 === 0;}

Warning : This might be exploiting a longstanding bug in Javascript and Actionscript [Number("1" + the_string) % 1 === 0)], I can't speak for that, but it is exactly what we needed.

查看更多
十年一品温如言
7楼-- · 2018-12-31 09:46

parseInt(), but be aware that this function is a bit different in the sense that it for example returns 100 for parseInt("100px").

查看更多
登录 后发表回答