Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

javascript - 验证JavaScript中的十进制数字-IsNumeric()(Validate decimal numbers in JavaScript - IsNumeric())

What's the cleanest, most effective way to validate decimal numbers in JavaScript?

(在JavaScript中验证十进制数字的最干净,最有效的方法是什么?)

Bonus points for:

(奖励积分:)

  1. Clarity.

    (明晰。)

    Solution should be clean and simple.

    (解决方案应该干净简单。)

  2. Cross-platform.

    (跨平台。)

Test cases:

(测试用例:)

01. IsNumeric('-1')      => true
02. IsNumeric('-1.5')    => true
03. IsNumeric('0')       => true
04. IsNumeric('0.42')    => true
05. IsNumeric('.42')     => true
06. IsNumeric('99,999')  => false
07. IsNumeric('0x89f')   => false
08. IsNumeric('#abcdef') => false
09. IsNumeric('1.2.3')   => false
10. IsNumeric('')        => false
11. IsNumeric('blah')    => false
  ask by community wiki translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@Joel's answer is pretty close, but it will fail in the following cases:

(@Joel的答案非常接近,但在以下情况下将失败:)

// Whitespace strings:
IsNumeric(' ')    == true;
IsNumeric('') == true;
IsNumeric('

') == true;

// Number literals:
IsNumeric(-1)  == false;
IsNumeric(0)   == false;
IsNumeric(1.1) == false;
IsNumeric(8e5) == false;

Some time ago I had to implement an IsNumeric function, to find out if a variable contained a numeric value, regardless of its type , it could be a String containing a numeric value (I had to consider also exponential notation, etc.), a Number object, virtually anything could be passed to that function, I couldn't make any type assumptions, taking care of type coercion (eg. +true == 1; but true shouldn't be considered as "numeric" ).

(前一段时间,我必须实现一个IsNumeric函数,以查明一个变量是否包含数值, 而不论其类型如何 ,它可能是一个包含数值的String (我还必须考虑指数表示法,等等), Number对象,几乎任何东西都可以传递给该函数,我不能做任何类型假设,要注意类型强制(例如+true == 1;但是true不应被视为"numeric" )。)

I think is worth sharing this set of +30 unit tests made to numerous function implementations, and also share the one that passes all my tests:

(我认为值得分享针对多种功能实现进行的这套+30单元测试 ,还应该分享通过我所有测试的测试:)

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

PS isNaN & isFinite have a confusing behavior due to forced conversion to number.

(由于强制转换为数字,因此PS isNaNisFinite的行为令人困惑。)

In ES6, Number.isNaN & Number.isFinite would fix these issues.

(在ES6中, Number.isNaNNumber.isFinite将解决这些问题。)

Keep that in mind when using them.

(使用它们时请记住这一点。)


Update : Here's how jQuery does it now (2.2-stable) :

(更新这是jQuery现在的方式(2.2稳定) :)

isNumeric: function(obj) {
    var realStringObj = obj && obj.toString();
    return !jQuery.isArray(obj) && (realStringObj - parseFloat(realStringObj) + 1) >= 0;
}

Update : Angular 4.3 :

(更新Angular 4.3 :)

export function isNumeric(value: any): boolean {
    return !isNaN(value - parseFloat(value));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...