【问题标题】:IsNumeric() similar function to js [duplicate]IsNumeric() 与 js 类似的功能 [重复]
【发布时间】:2015-02-02 19:12:58
【问题描述】:

我有一个从用户那里获取一些数字的代码。我想确保用户提供的数据是数字而不是字符串。根据我对 vb6 的了解,我知道我可以使用 IsNumeric,所以我想知道是否有任何与js类似的功能

【问题讨论】:

标签: javascript jquery html vb6


【解决方案1】:

使用isNaN() 并将字符串或数字传递给它。它将返回真或假。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN

【讨论】:

    【解决方案2】:

    根据之前在这里提出的问题: Is there any function like IsNumeric in javascript to validate numbers

    您可以在自己的库中找到一个优雅的函数,如下所示:

    function isNumeric(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    }
    

    【讨论】:

      【解决方案3】:

      试试

      var input = 1;
      console.log(typeof input === "number");
      

      typeof

      var input1 = 1, input2 = "1";
      console.log(typeof input1 === "number", typeof input2 === "number");

      【讨论】:

        猜你喜欢
        • 2013-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-05
        相关资源
        最近更新 更多