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

前端 未结 30 3347
-上瘾入骨i
-上瘾入骨i 2020-11-22 01:54

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

相关标签:
30条回答
  • 2020-11-22 02:41

    If you really want to make sure that a string contains only a number, any number (integer or floating point), and exactly a number, you cannot use parseInt()/ parseFloat(), Number(), or !isNaN() by themselves. Note that !isNaN() is actually returning true when Number() would return a number, and false when it would return NaN, so I will exclude it from the rest of the discussion.

    The problem with parseFloat() is that it will return a number if the string contains any number, even if the string doesn't contain only and exactly a number:

    parseFloat("2016-12-31")  // returns 2016
    parseFloat("1-1") // return 1
    parseFloat("1.2.3") // returns 1.2
    

    The problem with Number() is that it will return a number in cases where the passed value is not a number at all!

    Number("") // returns 0
    Number(" ") // returns 0
    Number(" \u00A0   \t\n\r") // returns 0
    

    The problem with rolling your own regex is that unless you create the exact regex for matching a floating point number as Javascript recognizes it you are going to miss cases or recognize cases where you shouldn't. And even if you can roll your own regex, why? There are simpler built-in ways to do it.

    However, it turns out that Number() (and isNaN()) does the right thing for every case where parseFloat() returns a number when it shouldn't, and vice versa. So to find out if a string is really exactly and only a number, call both functions and see if they both return true:

    function isNumber(str) {
      if (typeof str != "string") return false // we only process strings!
      // could also coerce to string: str = ""+str
      return !isNaN(str) && !isNaN(parseFloat(str))
    }
    
    0 讨论(0)
  • 2020-11-22 02:41

    I have tested and Michael's solution is best. Vote for his answer above (search this page for "If you really want to make sure that a string" to find it). In essence, his answer is this:

    function isNumeric(num){
      num = "" + num; //coerce num to be a string
      return !isNaN(num) && !isNaN(parseFloat(num));
    }
    

    It works for every test case, which I documented here: https://jsfiddle.net/wggehvp9/5/

    Many of the other solutions fail for these edge cases: ' ', null, "", true, and []. In theory, you could use them, with proper error handling, for example:

    return !isNaN(num);
    

    or

    return (+num === +num);
    

    with special handling for /\s/, null, "", true, false, [] (and others?)

    0 讨论(0)
  • 2020-11-22 02:43

    Why is jQuery's implementation not good enough?

    function isNumeric(a) {
        var b = a && a.toString();
        return !$.isArray(a) && b - parseFloat(b) + 1 >= 0;
    };
    

    Michael suggested something like this (although I've stolen "user1691651 - John"'s altered version here):

    function isNumeric(num){
        num = "" + num; //coerce num to be a string
        return !isNaN(num) && !isNaN(parseFloat(num));
    }
    

    The following is a solution with most likely bad performance, but solid results. It is a contraption made from the jQuery 1.12.4 implementation and Michael's answer, with an extra check for leading/trailing spaces (because Michael's version returns true for numerics with leading/trailing spaces):

    function isNumeric(a) {
        var str = a + "";
        var b = a && a.toString();
        return !$.isArray(a) && b - parseFloat(b) + 1 >= 0 &&
               !/^\s+|\s+$/g.test(str) &&
               !isNaN(str) && !isNaN(parseFloat(str));
    };
    

    The latter version has two new variables, though. One could get around one of those, by doing:

    function isNumeric(a) {
        if ($.isArray(a)) return false;
        var b = a && a.toString();
        a = a + "";
        return b - parseFloat(b) + 1 >= 0 &&
                !/^\s+|\s+$/g.test(a) &&
                !isNaN(a) && !isNaN(parseFloat(a));
    };
    

    I haven't tested any of these very much, by other means than manually testing the few use-cases I'll be hitting with my current predicament, which is all very standard stuff. This is a "standing-on-the-shoulders-of-giants" situation.

    0 讨论(0)
  • 2020-11-22 02:44

    2019: Practical and tight numerical validity check

    Often, a 'valid number' means a Javascript number excluding NaN and Infinity, ie a 'finite number'.

    To check the numerical validity of a value (from an external source for example), you can define in ESlint Airbnb style :

    /**
     * Returns true if 'candidate' is a finite number or a string referring (not just 'including') a finite number
     * To keep in mind:
     *   Number(true) = 1
     *   Number('') = 0
     *   Number("   10  ") = 10
     *   !isNaN(true) = true
     *   parseFloat('10 a') = 10
     *
     * @param {?} candidate
     * @return {boolean}
     */
    function isReferringFiniteNumber(candidate) {
      if (typeof (candidate) === 'number') return Number.isFinite(candidate);
      if (typeof (candidate) === 'string') {
        return (candidate.trim() !== '') && Number.isFinite(Number(candidate));
      }
      return false;
    }
    

    and use it this way:

    if (isReferringFiniteNumber(theirValue)) {
      myCheckedValue = Number(theirValue);
    } else {
      console.warn('The provided value doesn\'t refer to a finite number');
    }
    
    0 讨论(0)
  • 2020-11-22 02:44

    Using plain JavaScript:

    Number.isNaN(Number('1')); // false
    Number.isNaN(Number('asdf')); // true
    

    Using Lodash:

    _.isNaN(_.toNumber('1')); // false
    _.isNaN(_.toNumber('asdf')); // true
    
    0 讨论(0)
  • 2020-11-22 02:44

    PFB the working solution:

     function(check){ 
        check = check + "";
        var isNumber =   check.trim().length>0? !isNaN(check):false;
        return isNumber;
        }
    
    0 讨论(0)
提交回复
热议问题